← Pinterest Interview Insights
Started with a hash map to count impressions per ad, then a min-heap of size K to track the top results.
Start by clarifying requirements (e.g., data size, whether the class processes logs in batches or streams, and if K is fixed). Then design a class that aggregates counts using a hash map and uses a min-heap of size K to efficiently find the top-K ad IDs, discussing time and space complexity.
Pro tip: Mention that for streaming data, you can maintain the heap incrementally, and for batch processing, consider using a hash map followed by a heap or quickselect. Also, discuss how to handle ties and the possibility of using a count-min sketch for approximate results if memory is constrained.
Ask about input size, whether logs are processed in batches or as a stream, if K is fixed or variable, and if exact or approximate results are acceptable.
Use a hash map to count impressions per ad ID. For top-K, use a min-heap of size K to keep the K highest counts, or sort the counts if batch processing.
Outline methods: a constructor to initialize, a method to add a log entry (if streaming), and a method to get top-K ad IDs. Consider thread-safety if needed.
Explain time complexity: O(N log K) for streaming with heap, O(N + M log M) for batch with sorting (M unique ads). Space complexity: O(M + K).
Mention alternatives like quickselect for batch (O(N) average), using a count-min sketch for approximate top-K with limited memory, and handling ties or frequent updates.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by clarifying the requirements and constraints, then propose a sliding window approach using a time-ordered data structure like a deque or a circular buffer. Discuss how to efficiently evict stale data, either lazily on read or actively on write, and analyze the time and space complexity tradeoffs of your chosen design.
Pro tip: Mention that you can use a min-heap keyed by timestamp for eviction, but a deque is more efficient for FIFO eviction since timestamps are non-decreasing. Also, consider using a hash map from adId to a deque of timestamps for O(1) access to each ad's impressions.
Ask about the expected scale (number of ads, impressions per second), the definition of 'common ads' (top K by count?), and whether the window is fixed or sliding. Confirm that timestamps are monotonically increasing.
Propose a hash map from adId to a deque of timestamps for each ad's impressions within the window. Alternatively, use a global time-ordered structure like a deque of (timestamp, adId) pairs for eviction, combined with a count map.
For ingestImpressions, append the timestamp to the ad's deque and update counts. For eviction, remove timestamps older than the window from the front of deques, either lazily during getCommonAds or actively on each ingest.
To get top K ads, maintain a count of impressions per ad within the window. Use a min-heap of size K or quickselect to find the top K ads by count. Ensure stale data is evicted before counting.
Discuss time complexity: O(1) amortized for ingest and eviction, O(N) for getCommonAds if scanning all ads, or O(N log K) with heap. Space complexity: O(total impressions in window). Compare lazy vs active eviction and their impact on latency.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.