This one took me a while to even decompose properly.
Start by clarifying requirements and constraints, then propose a data structure that combines a hash map for O(1) access with a priority queue (heap) for efficient eviction. Explain how to handle global and per-file caps, and how to compute a composite score for eviction that balances priority and recency. Finally, discuss trade-offs and potential optimizations.
Pro tip: Mention that you would use a lazy deletion strategy in the heap to avoid O(n) removals, and that you would periodically rebuild the heap to maintain performance. This shows awareness of real-world implementation challenges.
Ask about expected scale, read/write patterns, and whether priorities are static or dynamic. Confirm that eviction should consider both priority and recency, and that per-file caps are optional.
Propose a hash map from log ID to log record for O(1) access, and a min-heap keyed by an eviction score (e.g., priority + timestamp) to find the least important log in O(1) amortized. For per-file caps, maintain a separate heap per file or a global heap with file filtering.
Explain how to compute the eviction score: higher priority means less likely to evict, and more recent logs are less likely to evict. Use a weighted sum or lexicographic ordering. Discuss how to handle updates to priority or recency (e.g., lazy updates).
On addLog, insert into hash map and heap, then check global and per-file caps. If exceeded, pop from heap until under cap, removing from hash map. Use lazy deletion: mark evicted logs and skip them when popped.
Insert: O(log n) for heap push. Eviction: O(log n) per eviction (amortized O(1) if using lazy deletion and occasional rebuild). Query: O(1) for ID lookup, O(k) for range queries. Discuss memory overhead and alternatives like balanced BSTs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.