Dsa
Stack
Medium
Transactional Storage Buffer
You are designing a transactional buffer for a database. The buffer supports five commands: 'PUSH x' (adds an integer x to the current scope), 'POP' (removes the last added element from the current scope), 'BEGIN' (starts a new nested transaction), 'COMMIT' (merges the current transaction's operations into the parent scope), and 'ROLLBACK' (discards all operations in the current scope and returns to the parent). If a 'POP' is called on an empty scope, ignore it. If 'COMMIT' or 'ROLLBACK' is called on the root scope, ignore it. Input consists of one command per line. Output the final state of the root scope as a space-separated sequence of integers.
Example:
PUSH 10
BEGIN
PUSH 20
PUSH 30
POP
COMMIT
Output: 10 20
Key concepts
stackrecursiondata-structures
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now