Lazy array with deferred map and indexOf
I was asked this
No reports yet. Sign in to say where you were asked it.
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 newLazyArraythat represents applyingfnto every element after all functions already recorded. It must not callfn, 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 equalstarget, or-1if there is none.
Constraints:
-
0 <= len(arr) <= 10^5; a chain may hold thousands ofmapcalls. - 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
indexOfthen raise or skip the element? - Are repeated
indexOfcalls on the same array common enough to cache results? - Should
mapchains share the recorded prefix with their parent, or copy it? - Is target equality plain
==on integers?
What a strong answer covers
- Represents each
LazyArrayas a reference to the shared base list plus a parent pointer or an immutable tuple of functions, somapis O(1) and never mutates the original. - Evaluates in
indexOfelement 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
astays 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 afterindexOf.
Follow-ups
- Cache evaluated values or memoise per (chain, index) for repeated queries.
- Add
filterand keepindexOfmeaning 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
- Databricks · Algorithms (two rounds) · Last asked Apr 202660 min · Live coding in CoderPad or similar
What it tests
design · lazy evaluation · memoization
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
