Implement Grid Spread and Transactional Store
LyftSoftware Engineer · OnsiteLast updated
This question evaluates skills in grid-based propagation modeling and stateful transactional system design, testing concepts such as graph traversal and timed spread dynamics for the grid task and nested transaction semantics, isolation, and state management for the in-memory key-value store.
You have two coding tasks.
Task A: Compute Contamination Spread Time
Given an m x n grid:
0means an empty cell.1means a fresh item.2means a contaminated item.
Every minute, each contaminated item contaminates its fresh 4-directional neighbors: up, down, left, and right.
Return the minimum number of minutes needed until no fresh items remain. If some fresh item can never be contaminated, return -1.
Handle edge cases such as an empty grid, no fresh items, isolated fresh items, and multiple initial contaminated items.
Task B: Implement an In-Memory Transactional Key-Value Store
Implement an in-memory key-value store supporting basic operations and transactions.
Required API:
get(key): Return the current value forkey, ornull/Noneif the key does not exist.set(key, value): Setkeytovaluein the current transaction if one exists; otherwise set it in the main store.delete(key): Deletekeyin the current transaction if one exists; otherwise delete it from the main store.begin(): Start a new transaction. Transactions may be nested.rollback(): Revert all changes made in the most recent active transaction. Return whether the rollback succeeded.commit(): Commit the most recent active transaction. If it is nested, merge its changes into the parent transaction; otherwise apply them to the main store. Return whether the commit succeeded.
Important edge cases:
rollback()orcommit()with no active transaction.- Reading a key changed in the current transaction.
- Reading a key changed in an outer transaction.
- Deleting a key inside a transaction and then rolling back.
- Setting a key after deleting it in the same transaction.
- Nested transactions with independent rollback and commit behavior.
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.