Kth Largest Element in a Stream — Min-Heap Design [Amazon Easy]
Design a class to find the kth largest element in a data stream. Min-heap of size k: the top is always the answer. Full C++, Java, JavaScript and Python.
webcoderspeed.com
59 articles
Design a class to find the kth largest element in a data stream. Min-heap of size k: the top is always the answer. Full C++, Java, JavaScript and Python.
Design a stack that pops the most frequent element (breaking ties by recency) using frequency and group-stacks mapping.
Answer election queries for who is leading at time t by precomputing the leader at each vote and binary searching.
Implement get(key, timestamp) using binary search on stored sorted timestamps for O(log n) retrieval.
Design a Least Recently Used cache with O(1) get and put operations using a HashMap combined with a doubly linked list.
Design a set with O(1) insert, delete, and getRandom using a dynamic array combined with a HashMap for index tracking.
Design a URL shortener using two hash maps for O(1) encode and decode operations.
Implement a hash map from scratch using an array of buckets with chaining for collision resolution.
Implement a hash set from scratch supporting add, remove, and contains in O(1) average time.
Design a time-stamped key-value store supporting set and get(key, timestamp) using binary search on sorted timestamps.
Implement an LFU cache with O(1) get/put using three hash maps: key→val, key→freq, freq→OrderedDict.
Design a data structure supporting inc/dec and getMaxKey/getMinKey in O(1) using a doubly linked list of frequency buckets.
Design Twitter with follow/unfollow and getNewsFeed using per-user tweet lists and a min-heap merge.
Design a simplified Twitter with follow/unfollow and a news feed showing the 10 most recent tweets from followed users.
Design a stack that pops the most frequent element, breaking ties by most recently pushed, using frequency-bucket stacks.
Design a seat manager that reserves the lowest numbered available seat and unreserves seats using a min-heap.
Flatten a multilevel doubly linked list by inserting child sub-lists inline using DFS or an explicit stack.
Implement a doubly linked list with sentinel head and tail nodes for clean O(1) insertions and deletions.
Implement LRU Cache from scratch using a doubly linked list with sentinel nodes and a hash map for O(1) operations.
Design a browser history with visit, back, and forward operations using a doubly linked list for O(1) navigation.
Implement a stack using one queue by rotating all elements after each push to bring the new element to the front.
Implement a queue using two stacks with amortized O(1) operations by lazily transferring from inbox to outbox.
Design a stack with O(1) getMin by maintaining a parallel min-tracking stack alongside the main stack.
Count ping requests within the last 3000ms using a queue that slides expired entries off the front.
Calculate the stock price span (consecutive days <= today) using a monotonic stack that accumulates spans.
Implement a circular queue using a fixed-size array with head and tail pointers and a size counter.
Implement an iterator for a nested list using a stack that lazily expands nested lists as elements are consumed.
Design a hit counter that returns hits in the past 5 minutes using a queue to expire old timestamps.
Design a frequency stack with O(1) push and pop-max-frequency using a map of frequency to stack of elements.
Find the median dynamically as numbers are added using two heaps: a max-heap for the lower half and min-heap for upper.
Serialize a binary tree to a string using BFS level-order and deserialize back using a queue for reconstruction.
Implement a Trie with insert, search, and startsWith operations. Core data structure for all prefix-based problems.
Trie that supports wildcard '.' matching any character. DFS through trie when '.' encountered, trying all children.
Query if any word ends at the current stream position. Insert reversed words into trie; maintain suffix deque to match from current position backwards.
Find word with given prefix AND suffix. Insert 'suffix#word' for each suffix of each word into trie; query combines prefix+suffix search.
Implement insert(key, val) and sum(prefix) returning sum of values for all keys with given prefix. Trie with value at end node.
Design an encode/decode scheme for a list of strings. Length-prefix encoding (4-byte header per string) is collision-proof unlike delimiter-based approaches.
Implement an iterator for a nested list that flattens it lazily. Meta favors this for testing iterator design, stack usage, and lazy evaluation.
Implement a read() function using a read4() primitive. Two variants: read once and read multiple times. Tests buffer management and pointer arithmetic.
Design a stream that always returns the kth largest element after each add. Uses a min-heap of size k — the top is always the kth largest.
Master system design problems that test data structure knowledge: LRU/LFU caches, Twitter feed, file systems, and 15+ design problems with full implementations.
Design a simplified Twitter where users post tweets and follow each other, with getNewsFeed returning the 10 most recent tweets from followed users using a min-heap merge.
Implement a file system that creates paths and associates values with them. Uses a Trie or HashMap to map full paths to values with O(L) operations.
Design a hit counter that counts hits in the last 5 minutes using a deque-based sliding window or circular buffer with O(1) amortised operations.
Design a key-value store that returns values at or before a given timestamp. Uses a hashmap of sorted (timestamp, value) lists with binary search for O(log n) get.
Design a search autocomplete system that returns top 3 historical queries matching the current prefix, sorted by frequency then lexicographically. Uses a Trie with per-node frequency maps.
Design a browser with visit, back, and forward navigation. Uses two stacks (or a doubly-ended array with pointer) for O(1) visit and O(steps) navigation.
Design a leaderboard that tracks player scores, supports score additions, top K sum queries, and player resets using a hashmap with sorted aggregation.
Simulate a snake game on a grid where the snake eats food to grow and dies if it hits walls or itself. Uses a deque for O(1) head/tail and a set for O(1) body collision checks.
Implement a skip list from scratch supporting search, add, and erase in O(log n) expected time. Uses layered linked lists with probabilistic level assignment.
Design a log storage system that retrieves log IDs within a timestamp range at a specified granularity (Year, Month, Day, Hour, Minute, Second).
Design a phone directory managing available and allocated numbers with O(1) get, check, and release using a queue of free numbers and a boolean availability array.
Design an in-memory key-value database that supports set, get, delete, and rank operations. Demonstrates combining hashmaps with sorted structures for efficient multi-key queries.
Implement a stack that supports push, pop, top, and retrieving the minimum element in O(1) time using an auxiliary min-stack that tracks minimums at each level.
Maintain a running median from a data stream using two heaps: a max-heap for the lower half and a min-heap for the upper half, rebalancing after each insertion.
Implement a circular queue (ring buffer) with fixed capacity supporting enQueue, deQueue, Front, Rear, isEmpty, and isFull in O(1) time using head and tail pointers.
Design a URL shortener like TinyURL that encodes long URLs to short codes and decodes them back. Uses base-62 encoding with a counter or random string generation.
Design a parking system with big, medium, and small spaces. O(1) addCar checks if space is available and decrements the counter, returning whether the car was parked.
Complete recap of all 20 system design DSA problems: pattern classification, time complexities, key data structures, and decision framework for choosing the right design approach.