Implement a DataBackend with an LRU Cache
Implement a bounded, in-memory LRU cache shared across all tables that evicts the least-recently-used row when full, then wire it into a DataBackend that orchestrates the cache with a durable persistent store, lazily creating tables by inferring their schema on first write.
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Late October, 2026
PART 1: #pragma once #include <optional> #include <stdexcept> #include <string> #include <bits/stdc++.h> using namespace std; #include "value.hpp" // A bounded, in-memory key-value store with LRU eviction, SHARED by all tables. // memorySize caps how many rows are held IN TOTAL across every table; the least-recently-used row // is evicted once full, and the victim may belong to a different table than the row being written. // Recency must update on both get and set. The cache knows nothing about tables or schemas. class LRUCache { public: explicit LRUCache(int memorySize) { this->memorySize = memorySize; } std::optional<Row> get(const std::string& table, const std::string& key) { } void set(const std::string& table, const std::string& key, Row value) { } private: // use variables as you see fit }; ------------------ #pragma once #include <stdexcept> #include <string> #include "lru_cache.hpp" #include "persistent_store.hpp" #include "value.hpp" #include "persistent_store.hpp" // The object main() talks to. It orchestrates the in-memory cache and (Part 2) the durable // PersistentStore. There is no createTable: the caller only ever calls get and set. class DataBackend { public: explicit DataBackend(int memorySize): { } GetResult get(const std::string& table, const std::string& key) { } // TODO (Part 2): a write to a table you have not seen yet has to create it in the store first // -- infer the schema from `value` (see inferSchema in value.hpp), and remember // which tables you already created so you skip the DDL on later writes. void set(const std::string& table, const std::string& key, const Row& value) { } private: };
Hello Interview Premium
Your account is free and you can post anonymously if you choose.