Lazy Array (Lazy Evaluation) with Unit Tests
Question Details
Question: Implement a Lazy Array (Lazy Evaluation) with Unit Tests
Implement a LazyArray that supports lazy evaluation. It takes a function to generate elements on demand, and must not gener
Full Details
Question: Implement a Lazy Array (Lazy Evaluation) with Unit Tests
Implement a LazyArray that supports lazy evaluation. It takes a function to generate elements on demand, and must not generate all elements upfront. When an index is accessed for the first time, compute it via the function and cache it.
Requirements
Implement LazyArray:
- Constructor:
LazyArray(n: int, f: Callable[[int], Any]) nis the array length.f(i)generates the element at indexi.get(i: int) -> Any
-
Return the element at index i.
- On first access, call f(i) and cache.
- Subsequent accesses to the same index must not call f(i) again.
- set(i: int, value: Any) -> None
- Set index i to value and override the cache (later get(i) must not call f(i)).
Constraints
- Define out-of-bounds behavior clearly (exception or error value).
f(i)can be expensive; avoid unnecessary calls.- Thread-safety is not required unless you choose to support it.
Unit Tests
Cover at least:
- Laziness: constructor does not call
f; only the firstget(i)calls it once. - Caching: repeated
get(i)does not callf(i)again. - After
set(i, value),get(i)
returns the new value and does not call f(i).
4. Out-of-bounds behavior.
Scale
n: 1 <= n <= 1,000,000- Number of operations
Q: 1 <= Q <= 200,000
I/O (online-judge style)
Input:
- First line:
n Q - Second line: integer
seed(used by generator) - Next
Qlines: GET iSET i value
Generator function is defined as:
f(i) = (i * 1315423911 + seed) % 1000000007
Output:
- For each
GET, print the element value
Sample Input
5 5
7
GET 0
GET 0
SET 0 42
GET 0
GET 1
Sample Output
7
7
42
918877915
Test Cases
Case 1
Input:
5 5
7
GET 0
GET 0
SET 0 42
GET 0
GET 1
Output:
7
7
42
918877915
Case 2
Input:
3 4
1
GET 2
GET 1
GET 2
GET 0
Output:
633084775
315421140
633084775
1
Case 3
Input:
2 3
10
SET 1 99
GET 1
GET 0
Output:
99
10
Case 4
Input:
1 2
123
GET 0
GET 0
Output:
123
123
Case 5
Input:
4 6
5
GET 3
SET 3 8
GET 3
GET 2
SET 2 8
GET 2
Output:
944299085
8
628858?
8
About This Question
This is a reported interview question from a databricks interview for a swe role during the coding round.
It covers the following topics: System Design, Arrays .
Topics
More Databricks Interview Questions
About Databricks Interview Reports
This question was reported by a candidate who interviewed at Databricks. LeakCode aggregates interview reports from 10+ sources, including 1Point3Acres, Glassdoor, LeetCode Discuss, Blind, Reddit, Indeed, and Nowcoder. Each report is translated where necessary, deduplicated against existing entries, and tagged by company, role, round type, and reporting date.
Use this question as one calibration data point, not a memorization target. Companies typically rotate their question pools every 2-4 months; the exact wording of a 2024 question may differ from what you encounter today. The underlying pattern, difficulty level, and follow-up depth at Databricks are the higher-signal extractions to take from this report.
For broader preparation context, the Databricks interview process typically includes a recruiter screen, one or two technical phone screens, and a 4-5 round on-site loop covering coding, system design (at L4+ levels), and behavioral. Reports tagged on LeakCode show the round-by-round distribution and typical difficulty calibration. To browse questions filtered by round type and seniority, use the company hub linked above.
How To Practice This Type of Question
Solve similar problems on LeetCode under timed conditions (25-35 minutes per medium difficulty). The goal is pattern recognition: recognize the underlying technique (sliding window, two-pointer, BFS, memoized recursion, etc.) within 60-90 seconds of reading. Strong candidates verbalize their hypothesis out loud before coding, then iterate based on feedback. Weak candidates dive into implementation immediately, lose time on the wrong approach, and run out of time for follow-ups.
Companies update their question pools every 2-4 months. The exact wording of any given question may have been retired by the time you interview. Focus your prep on the pattern, not the specific problem. The patterns that appear in Databricks reports consistently are the ones worth investing in; one-off niche problems are not.
During Your Databricks Round
Apply the standard interview round template: clarify requirements (2-3 minutes), state your approach out loud and confirm direction with the interviewer (3-5 minutes), code with narration (15-25 minutes), test with concrete examples including edge cases (5 minutes), discuss optimization or trade-offs if time permits (5 minutes). This template is universally accepted across FAANG and adjacent companies; deviating from it produces weaker interviewer feedback signal.
The single most predictive failure mode in Databricks reports tagged "no hire": not asking clarifying questions. Interviewers are explicitly trained to weight this. Strong candidates ask 3-5 clarifying questions even on problems that look obvious; weak candidates dive into code immediately. The clarifying-question check is often the first signal recorded in the interviewer's written notes.