Katkat

Implement Grid Spread and Transactional Store

LyftSoftware Engineer · Onsite

Last 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:

  • 0 means an empty cell.
  • 1 means a fresh item.
  • 2 means 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 for key, or null/None if the key does not exist.
  • set(key, value): Set key to value in the current transaction if one exists; otherwise set it in the main store.
  • delete(key): Delete key in 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() or commit() 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.