Skip to content

smartacteam/kotlin-transaction-challenge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Transactional Key-Value Store

Problem Statement

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.

Getting Started

Prerequisites

  • Java 17 or higher
  • Gradle 8.2+ (or use the included Gradle wrapper)

How to run the test

# Run tests
./gradlew test

Requirements

Implement a TransactionalStore class with the following methods:

Core Operations

  • set(key: String, value: Int): Store a value for the given key
  • get(key: String): Int?: Return the value for the key, or null if not found
  • delete(key: String): Boolean: Remove the key from the database, returns true if key existed

Transaction Operations

  • begin(): Start a new transaction (can be nested)
  • commit(): Boolean: Save changes from the current transaction to the parent scope
  • rollback(): Boolean: Discard all changes made in the current transaction

Rules:

  1. Changes in a transaction are visible within that transaction
  2. commit() merges changes into the parent transaction (or global state)
  3. rollback() discards all changes in the current transaction
  4. commit() or rollback() with no active transaction should return false
  5. Nested transactions are supported—each begin() creates a new scope

Example

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

Constraints

  • 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

Evaluation Criteria

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages