Implement two coding tasks:
-
Transaction pagination: Given an in-memory collection of transaction records with fields (txn_id: string, user_id: string, amount: int, created_at: int epoch seconds), implement a cursor-based pagination API get_page(user_id, page_size, cursor) -> {items, next_cursor}. Return transactions for the user ordered by created_at descending, breaking ties by txn_id ascending. The cursor must be opaque and ensure stable pages even if new transactions are inserted after earlier pages are fetched (no duplicates or skips). Explain your data structures, cursor encoding/decoding, and the time/space complexity.
-
Time-versioned key-value store: Design and implement a store with set(key, value, t) and get(key, t) that returns the value with the greatest timestamp ≤ t, or null if none exists. Optimize for heavy reads (millions of queries). Choose data structures to achieve O(log n) per operation per key, handle duplicate timestamps deterministically, and discuss memory trade-offs and concurrency safety for readers.