Driver Balance Ledger With Nested Transactions and Rollback

Last updated: November 29, 2026

Quick Overview

Implement an in-memory ledger that processes commands SET driver amount, GET driver, UNSET driver, COUNT amount (drivers with exactly that balance), BEGIN, ROLLBACK and COMMIT, where transactions can nest. Return the outputs of GET, COUNT, and any ROLLBACK or COMMIT with no open transaction.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement an in-memory ledger that processes commands SET driver amount, GET driver, UNSET driver, COUNT amount (drivers with exactly that balance), BEGIN, ROLLBACK and COMMIT, where transactions can nest. Return the outputs of GET, COUNT, and any ROLLBACK or COMMIT with no open transaction.

A command interpreter in the style of Uber's onsite coding, framed around driver earnings adjustments that must be reversible. The interviewer is checking an undo log per transaction level and an O(1) COUNT via a reverse index.

What the Interviewer Expects
  • Parses commands and returns outputs in order
  • Keeps a reverse index from value to count for O(1) COUNT
  • Uses a stack of undo logs, one per BEGIN
  • Records only the first prior value per key per level
  • Treats COMMIT as closing every open transaction
Key Topics to Cover
State machines
Hash maps
Undo log
Transactions
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How does the memory use of an undo log compare to copying the map at BEGIN?
  • How would you make COMMIT close only the innermost transaction?
  • How would you persist this ledger so a crash does not lose committed data?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Commands are strings. GET prints the balance or NULL. COUNT v prints how many drivers have balance v. ROLLBACK undoes the innermost open transaction; COMMIT makes all open transactions per...

High-Level Design

Keep data (driver to balance) and counts (balance to number of drivers). Each BEGIN pushes an empty dict onto a stack. Before any write, if a transaction is open and the key is not yet in the to...


Submit Your Answer
Markdown supported