Katkat

Implement lazy array and KV store

DatabricksSoftware Engineer · Onsite

Last 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 be O(1) until compute() is called. compute should be O(N * M) where N is the number of elements and M is the number of operations.
  • KVStore: All operations should aim for O(1) average time complexity.
  • Consider empty inputs for LazyArray and requesting counts for keys that were never put.

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?