Problemlazy mapped array

Session: Sign in to solve

Solution.txt

Lazy Mapped Array

Problem

Design LazyMappedArray, an integer array that remembers mapping functions and evaluates them only when a search needs transformed values.

The object keeps the source positions fixed. A search walks those positions from left to right, applies the current transformation chain to each examined value, and stops at the first match.

Requirements

  • Deferred mapping. map records the supplied integer-to-integer callable without invoking it, returns the same LazyMappedArray, and includes it in later searches.
  • Mapping order. For each examined source value, apply every registered callable exactly once in map call order, passing each result to the next callable.
  • First match. getIndexOf returns the smallest zero-based source index whose fully transformed value equals the target, or -1 when no value matches. An empty source array also returns -1.
  • Early stop. After finding the first match, getIndexOf returns without invoking any callable for a later source index. A missing search examines every source index.

API

SignatureReturnsBehavior
LazyMappedArray(values)A new LazyMappedArrayStores the ordered signed 64-bit source values with no registered transformations.
map(transformation)This same LazyMappedArray instanceRegisters a callable from one signed 64-bit integer to one signed 64-bit integer without invoking it.
getIndexOf(value)The first matching zero-based index, or -1Lazily applies the current transformation chain while searching for the signed 64-bit target.

Examples

Start with [1, 2, 3, 2] and call map(x -> x * 2). The callable is not invoked by map. A later getIndexOf(4) transforms the values at indices 0 and 1, obtains 2 and 4, and returns 1. It does not transform indices 2 or 3.

For [1, 2], the chain map(x -> x + 3).map(x -> x * 2) produces logical values 8 and 10. Therefore, getIndexOf(10) returns 1.

Constraints

  • 0 <= values.length <= 1000
  • At most 50 calls to map per object
  • At most 500 calls to getIndexOf per object
  • Source values, targets, and every intermediate callable result fit a signed 64-bit integer
  • Every supplied callable is valid and returns normally for the tested inputs
  • Calls are sequential

Notes

The custom-test JSON represents a callable with integer multiply and add fields. The driver converts that object to x -> multiply * x + add before calling map. This transport representation is not part of the learner API; map receives a callable.

No behavior is required for caching results across searches.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.