Implement lazy array and KV store
DatabricksSoftware Engineer · OnsiteLast updated
This problem tests your ability to implement lazy computation using function composition and design a stateful data structure with metadata tracking.
Part 1: LazyArray
Implement a LazyArray class that supports a sequence of transformations without executing them until the values are explicitly requested. The class should store an initial base list and a list of transformation functions (e.g., map, filter).
__init__(self, data): Initializes the array.map(self, func): Chains a transformation to be applied to every element.filter(self, predicate): Chains a filter to keep only elements satisfying the predicate.compute(self): Applies all chained operations in order and returns the resulting list.
Example:
arr = LazyArray([1, 2, 3, 4])
arr.map(lambda x: x * 2).filter(lambda x: x > 5)
print(arr.compute()) # Output: [6, 8]
Part 2: Instrumented KV Store
Design a key-value store class KVStore that maintains a count of how many times each key is accessed (via get).
put(key, value): Stores a value.get(key): Returns the value and increments its access count.get_hit_count(key): Returns the current access count for the key.
Constraint: Ensure operations are efficient. Consider how the store handles missing keys.
Constraints & Edge Cases
LazyArray: Operations should beO(1)untilcompute()is called.computeshould beO(N * M)where N is the number of elements and M is the number of operations.KVStore: All operations should aim forO(1)average time complexity.- Consider empty inputs for
LazyArrayand requesting counts for keys that were neverput.
Hints
Hint 1
For LazyArray, store the transformations as a list of functions and iterate through this list when compute() is invoked.
Hint 2
For the KVStore, use two separate dictionaries: one to map keys to values, and another to map keys to access integers.
Hint 3
Ensure the LazyArray handles chaining correctly by passing the intermediate output of each function into the next.
Follow-up questions
- How would you modify LazyArray to support a 'limit' operation efficiently?
- If the KVStore needs to handle multi-threading, what concurrency control mechanisms would you implement?
- How could you implement an LRU eviction policy for the KVStore while maintaining the hit counting requirement?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.