Start by clarifying requirements and constraints, then design a data structure that maps keys to nested maps of field-value pairs. Implement the operations with careful handling of edge cases like missing keys and deletions, and discuss time/space complexity and potential improvements.
Pro tip: Mention that you would use a concurrent-safe map or add synchronization for thread safety, as this is critical in backend systems. Also, discuss how you would extend this to support persistence or TTL, showing foresight.
Ask about expected operations, data types, concurrency needs, and performance requirements. Confirm whether keys and fields are strings and if values can be any type.
Propose using a hash map (e.g., HashMap<String, Map<String, String>>) to store keys and their field-value maps. Discuss alternatives like a custom class for entries.
Write pseudocode or code for get, set, and delete. For get, return the value or null if key/field missing. For set, create nested map if key absent. For delete, remove field and optionally the key if empty.
Address missing keys, null inputs, and concurrent modifications. Discuss whether to remove empty maps after deletion to save memory.
State average O(1) time for operations. Mention potential enhancements like thread safety, persistence, or TTL.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Sorted the keys on output rather than keeping them sorted at insert time.
Start by clarifying the data model and requirements: what is the key type, what are fields, and what is the expected scale? Then propose a data structure that supports efficient ordered scans, such as a sorted map (e.g., balanced BST or skip list) or a trie for prefix scans, and explain how to implement scan and scan-by-prefix to return fields in lexicographical order. Finally, discuss trade-offs, concurrency, and persistence considerations.
Pro tip: Mention that lexicographical order should be based on the field names, not values, and consider using a composite key (main key + field name) in a sorted store to enable efficient range scans. Also, highlight the importance of defining the behavior for non-existent keys and empty prefixes.
Ask about the data model (key type, field types), expected scale (number of keys, fields per key), and performance requirements (latency, throughput). Confirm that lexicographical order applies to field names.
Propose a sorted map (e.g., balanced BST, skip list, or B-tree) for storing fields per key, or a trie for prefix scans. If using a single global sorted store, suggest a composite key of (main key, field name) to enable efficient range scans.
For scan, retrieve all fields for a given key by iterating over the sorted fields in order. If using a global store, perform a range scan from (key, minField) to (key, maxField).
For scan-by-prefix, iterate over fields that start with the given prefix. In a sorted map, find the first field >= prefix and iterate until fields no longer start with the prefix. In a trie, traverse to the prefix node and collect all descendant fields.
Address concurrency (e.g., read-write locks, MVCC), persistence (e.g., LSM trees, B-trees), and performance (e.g., caching, bloom filters). Mention edge cases like empty prefix, non-existent key, and large result sets.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by clarifying the current data model and operations, then propose extending each operation to accept an optional timestamp parameter, defaulting to server time. For TTL, design a set operation that stores an expiration timestamp per field and implement lazy expiration on read plus a background sweeper for cleanup. Discuss trade-offs between precision, memory overhead, and performance.
Pro tip: Mention that TTLs should be stored as absolute timestamps (not relative) to avoid clock drift issues, and highlight the importance of idempotency and monotonicity when clients supply timestamps.
Ask about consistency needs, clock synchronization, and whether timestamps are client-supplied or server-generated. Confirm if TTL is per-field or per-key and if expiration should be exact or eventual.
Modify each operation (get, set, delete, etc.) to accept an optional timestamp parameter. Define behavior when timestamp is omitted (use server time) and how to handle out-of-order or future timestamps.
Add a set operation that accepts a TTL in seconds/milliseconds. Store the expiration time as an absolute timestamp alongside the value. Consider per-field TTLs if the data model supports fields.
Choose between lazy expiration (check on read) and active expiration (background sweeper). Discuss hybrid approach: lazy on read for correctness, periodic sweep for memory reclamation.
Discuss memory overhead of storing expiration times, precision vs. performance, clock skew handling, and how to avoid thundering herd on expiration. Mention monitoring and metrics.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
The TTL recalculation after restore is the nasty bit.
Start by clarifying requirements: backup frequency, acceptable data loss window (RPO), restore time (RTO), and whether backups are full or incremental. Then propose a design that captures the full database state including TTLs, likely using a snapshot mechanism with a write-ahead log or change data capture for point-in-time recovery. Discuss trade-offs between storage cost, backup duration, and restore complexity, and outline how to handle TTLs during restore.
Pro tip: Emphasize that TTLs must be stored as absolute expiration timestamps, not relative durations, so that after restore the remaining TTL is correctly computed based on the restore time. Also mention that you'd validate backups by periodically restoring them in a staging environment to ensure integrity.
Ask about RPO, RTO, backup frequency, retention policy, and whether the database supports snapshots. Determine if backups need to be incremental or full, and if point-in-time recovery is required.
Propose taking periodic full snapshots (e.g., daily) plus continuous incremental backups (e.g., WAL shipping or CDC). Ensure each backup records the timestamp and includes TTL information as absolute expiration times.
To restore to a point in time, find the most recent full backup before that time, then apply incremental changes up to the target time. Recompute remaining TTLs based on the restore timestamp and filter out expired keys.
Store TTLs as absolute Unix timestamps in the backup. During restore, for each key, if the expiration timestamp is in the past, skip it; otherwise, set the new TTL as expiration - current_time.
Compare storage overhead, backup/restore performance, and complexity. Mention monitoring, encryption, and testing restores regularly. Consider using existing database tools (e.g., Redis RDB/AOF, PostgreSQL PITR) if applicable.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.