← Questions

Lazy array with deferred map and indexOf

0
DatabricksCodingLast asked Apr 2026

A coding interview question asked in Databricks's algorithms (two rounds) round, last asked Apr 2026. It tests design, lazy evaluation and memoization.

Implement LazyArray, an integer array that supports deferred mapping. Calls to map are recorded but do not run. The recorded functions run only when a query needs a value.

Implement:

  • LazyArray(arr): wraps a list of integers.
  • map(fn) -> LazyArray: returns a new LazyArray that represents applying fn to every element after all functions already recorded. It must not call fn, and it must not change the array it was called on. Calls can be chained, and one array can be mapped more than once into independent chains.
  • indexOf(target) -> int: applies the recorded functions in order and returns the smallest index whose final value equals target, or -1 if there is none.

Constraints:

  • 0 <= len(arr) <= 10^5; a chain may hold thousands of map calls.
  • Mapping functions are pure: same input, same output, no side effects.
  • Do no more work than a query needs. Be ready to explain how you would test that evaluation is really lazy.

Example 1

a = LazyArray([3, 7, 11, 15])
b = a.map(lambda x: x * 3)      # no function runs yet
b.indexOf(21)                   # 1   (values are [9, 21, 33, 45])
b.map(lambda x: x + 1).indexOf(34)  # 2
a.indexOf(11)                   # 2   (a is unchanged)

Example 2

c = LazyArray([5, 8, 5, 8]).map(lambda x: x % 2)
c.indexOf(1)    # 0   (first occurrence)
c.indexOf(2)    # -1
LazyArray([]).map(lambda x: x).indexOf(0)   # -1

Clarifying questions

  • Can mapped functions raise, and should indexOf then raise or skip the element?
  • Are repeated indexOf calls on the same array common enough to cache results?
  • Should map chains share the recorded prefix with their parent, or copy it?
  • Is target equality plain == on integers?

What a strong answer covers

  • Represents each LazyArray as a reference to the shared base list plus a parent pointer or an immutable tuple of functions, so map is O(1) and never mutates the original.
  • Evaluates in indexOf element by element, applying the chain and stopping at the first match, so it does no work past the answer: O(i * f) for match index i and chain length f.
  • Keeps independent branches of the same array correct (Example 1's a stays unchanged).
  • Handles an empty array, no match (-1) and duplicates (first index).
  • Tests laziness with a counting or recording function: zero calls after map, and exactly the needed calls after indexOf.

Follow-ups

  • Cache evaluated values or memoise per (chain, index) for repeated queries.
  • Add filter and keep indexOf meaning the index in the filtered result.
  • Fuse a long chain into one function, and discuss the recursion depth of nested lambdas.

Where it's asked

What it tests

design · lazy evaluation · memoization

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding