Third Maximum Number — OrderedSet O(n) [Microsoft Easy]
Return the third distinct maximum or the max if fewer than 3 distinct values exist. Three-variable tracking O(n) O(1) solution.
20 articles
Return the third distinct maximum or the max if fewer than 3 distinct values exist. Three-variable tracking O(n) O(1) solution.
Learn why 3Sum is a FAANG interview staple — how sorting enables two pointers, why deduplication trips up even strong candidates, a full visual dry run, and every common bug explained with Python and JavaScript solutions.
Master the classic Merge Intervals problem (LeetCode 56) asked at Google, Meta, Amazon, and Microsoft. Learn why sorting by start time is the key insight, the exact overlap condition (c ≤ b), a step-by-step visual dry run, 4 common mistakes, Python and JavaScript solutions, and follow-up problems including Insert Interval, Non-overlapping Intervals, and Meeting Rooms II.
LeetCode 347 asks for k most frequent elements with a constraint: beat O(n log n). Learn why naive sort fails, how a min-heap of size k achieves O(n log k), and the elegant bucket sort insight that delivers true O(n) — with full visual dry run, common mistakes, and Python/JavaScript solutions for all three approaches.
Rearrange an array so nums[0] `<` nums[1] > nums[2] `<` nums[3]... using virtual index mapping and nth_element for O(n) time.
Rearrange nums to maximize advantage over B using a greedy strategy: assign the smallest winning card, else discard the smallest.
Find the shortest subarray that when sorted makes the whole array sorted, using a single linear scan tracking violated boundaries.
Count spell-potion pairs with product >= success by sorting potions and binary searching for each spell's threshold.
Find the maximum number of nested envelopes by sorting by width ascending then height descending, then applying LIS.
Assign gold/silver/bronze or rank numbers to athletes based on their scores using sorted ordering.
For each query point, find the smallest interval containing it by sorting both intervals and queries, using a min-heap.
Sort array elements by frequency ascending, breaking ties by value descending.
Find the kth largest level sum in a binary tree using BFS to compute level sums then sorting or using a heap.
Group tree nodes by vertical column then row, sorting by value within same position using BFS with coordinates.
Find minimum swaps to sort each level of a binary tree using BFS and cycle-detection in permutation sorting.
Find all unique triplets summing to zero by sorting and using two pointers for each fixed element with careful duplicate skipping.
Find the maximum frequency of any element after at most k increments by sorting and using a sliding window with a running sum.
Find the minimum difference between the max and min of any k scores by sorting and using a fixed window of size k.
Build a suffix array in O(n log²n) using doubling sort. Enables O(m log n) substring search, longest common substring, and number of distinct substrings.
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.