annotate grok_interview/transaction.py @ 170:7fce234bfdca

Closing old fzf head
author MrJuneJune <me@mrjunejune.com>
date Mon, 19 Jan 2026 17:35:39 -0800
parents 68fa88ac73fe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 # Question: In-Memory Key-Value Store with Nested Transactions
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2 #
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
3 # Design an in-memory key-value database that supports the following commands and fully handles nested transactions.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
4 # Core Operations:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
5
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
6 # SET <key> <value>: Sets the value for a key.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
7
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
8 # GET <key>: Returns the value for a key.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
9
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
10 # UNSET <key>: Removes the key and its value.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
11 #
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
12 # Transaction Operations:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
13
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
14 # BEGIN: Starts a new transaction scope. If a transaction is already active, this starts a nested transaction.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
15
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
16 # COMMIT: Applies all changes made in the current transaction scope and all its active nested transactions to the parent scope (or to the main database state if no parent exists). After a successful commit, the transaction scope is closed.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
17
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
18 # ROLLBACK: Discards all changes made in the current transaction scope and all its active nested transactions, restoring the state to what it was when the BEGIN command was issued for the current scope. After a successful rollback, the transaction scope is closed.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
19
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
20 # Implementation Goal:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
21 # Design the primary data structures and outline the logic for SET, COMMIT, and ROLLBACK to ensure nested transactions operate correctly. Explain how the state of the database is managed across multiple transaction layers.
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
22
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
23
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
24 class Database:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
25 def __init__(self):
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
26 self.global_db = {}
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
27 self.queue = []
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
28
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
29 def commit(self):
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
30 copy_db = self.queue.pop()
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
31 if self.queue:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
32 self.queue[-1].merge(copy_db)
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
33 else:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
34 self.global_db |= copy_db
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
35
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
36 def set(self, key, value):
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
37 if self.queue:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
38 curr_db = self.queue[-1]
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
39 curr_db[key] = value
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
40 else:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
41 self.global_db[key] = value
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
42
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
43 def get(self, key):
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
44 if self.queue:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
45 for copy_db in reversed(self.queue):
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
46 if key in copy_db:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
47 return copy_db[key]
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
48
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
49 return self.global_db[key]
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
50
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
51 def unset(self, key):
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
52 if self.queue:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
53 curr_db = self.queue[-1]
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
54 del curr_db[key]
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
55 else:
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
56 del self.global_db[key]
68fa88ac73fe Interview prep for xAI
June Park <parkjune1995@gmail.com>
parents:
diff changeset
57