You need to build a simple in-memory database that supports basic Get/Set/Delete operations. The critical feature is support for nested transactions with commit and rollback capabilities.
This pattern is fundamental to database systems and tests understanding of state management, stack-based data structures, and edge case handling.
- Java 17 or higher
- Gradle 8.2+ (or use the included Gradle wrapper)
# Run tests
./gradlew testImplement a TransactionalStore class with the following methods:
set(key: String, value: Int): Store a value for the given keyget(key: String): Int?: Return the value for the key, ornullif not founddelete(key: String): Boolean: Remove the key from the database, returns true if key existed
begin(): Start a new transaction (can be nested)commit(): Boolean: Save changes from the current transaction to the parent scoperollback(): Boolean: Discard all changes made in the current transaction
Rules:
- Changes in a transaction are visible within that transaction
commit()merges changes into the parent transaction (or global state)rollback()discards all changes in the current transactioncommit()orrollback()with no active transaction should returnfalse- Nested transactions are supported—each
begin()creates a new scope
val db = TransactionalStore()
db.set("a", 10)
println(db.get("a")) // Returns 10
db.begin() // Transaction 1
db.set("a", 20)
println(db.get("a")) // Returns 20
db.begin() // Transaction 2 (Nested)
db.delete("a")
println(db.get("a")) // Returns null
db.rollback() // Rollback Transaction 2
println(db.get("a")) // Returns 20 (Back to Transaction 1 state)
db.commit() // Commit Transaction 1 to Global
println(db.get("a")) // Returns 20- Keys: Non-empty strings, length 1 to 100
- Values: Any integer (-10^9 to 10^9)
- Operations: Up to 100,000 total operations
- Nesting depth: Up to 1,000 nested transactions
- Correctness: All operations work correctly with nested transactions
- Efficiency: O(1) for get/set/delete, efficient commit/rollback
- Code Quality: Clean implementation with proper state management
- Edge Cases: Empty transactions, delete non-existent key, commit/rollback without transaction