The classic stock problem asked at Amazon, Google and Microsoft. Learn the O(n) greedy "min so far" approach with complete solutions in C, C++, Java, JavaScript and Python, plus extensions to Stock II, III and IV.
March 19, 2026 Read →
Move all zeroes to end while maintaining relative order of non-zero elements. Optimal O(n) two-pointer solution with minimal writes. Full C, C++, Java, JavaScript and Python code.
March 19, 2026 Read →
Merge two sorted arrays in-place by filling from the end. The classic 3-pointer technique asked at Meta and Microsoft. Full solutions in C, C++, Java, JavaScript and Python.
March 19, 2026 Read →
Remove duplicates from a sorted array in-place using the write pointer pattern. O(n) time, O(1) space. Full solutions in C, C++, Java, JavaScript, Python with follow-up for allowing k duplicates.
March 19, 2026 Read →
Return the third distinct maximum or the max if fewer than 3 distinct values exist. Three-variable tracking O(n) O(1) solution.
March 19, 2026 Read →
Rotate array right by k steps. Triple reverse trick: reverse whole, reverse first k, reverse rest. O(n) time O(1) space.
March 19, 2026 Read →
Find the minimum element in a rotated sorted array with no duplicates. Binary search: the minimum is in the unsorted half. O(log n) time O(1) space.
March 19, 2026 Read →
Find the lexicographically next permutation in-place using a two-pass scan from the right.
March 19, 2026 Read →
Sort an array of 0s, 1s, and 2s in-place in one pass using Dijkstra's Dutch National Flag algorithm.
March 19, 2026 Read →
Generate all permutations of distinct integers using in-place swap backtracking.
March 19, 2026 Read →
Find the smallest contiguous subarray with sum >= target using the shrinkable sliding window technique.
March 19, 2026 Read →
Search for a word in a 2D grid using DFS backtracking with in-place visited marking.
March 19, 2026 Read →
Find minimum number of jumps to reach the last index using greedy BFS-style level tracking.
March 19, 2026 Read →
Find the missing number in [0..n] using the Gauss sum formula or XOR in O(n) time O(1) space.
March 19, 2026 Read →
Calculate trapped rain water using inward two pointers that track left-max and right-max, replacing the classic O(n) space approach.
March 19, 2026 Read →
Find the maximum in every sliding window of size k in O(n) using a monotonic decreasing deque.
March 19, 2026 Read →
Find the smallest missing positive integer in O(n) time O(1) space using in-place cyclic sort to place each number at its correct index.
March 19, 2026 Read →
Find the largest rectangle in a histogram in O(n) using a monotonic increasing stack that computes area when a shorter bar is encountered.
March 19, 2026 Read →
Find the median of two sorted arrays in O(log(min(m,n))) by binary searching for the correct partition point.
March 19, 2026 Read →
Find the largest rectangle of 1s in a binary matrix by treating each row as a histogram and applying the Largest Rectangle in Histogram algorithm.
March 19, 2026 Read →
Find two indices that add to target in O(n) by storing each number in a HashMap and looking up the complement.
March 19, 2026 Read →
Check if two strings are anagrams by comparing their character frequency counts using an array or HashMap.
March 19, 2026 Read →
Determine if a number is happy by repeatedly summing digit squares and detecting cycles using a HashSet or Floyd algorithm.
March 19, 2026 Read →
Group strings that are anagrams together by using their sorted form as a HashMap key.
March 19, 2026 Read →
Find the k most frequent elements in O(n) using bucket sort by frequency instead of a heap.
March 19, 2026 Read →
Design a Least Recently Used cache with O(1) get and put operations using a HashMap combined with a doubly linked list.
March 19, 2026 Read →
Check if a string is a palindrome after removing non-alphanumeric characters using inward two pointers.
March 19, 2026 Read →
Reverse an array of characters in-place using the classic two-pointer swap technique.
March 19, 2026 Read →
Remove duplicates in-place from a sorted array using a write pointer that only advances on unique elements.
March 19, 2026 Read →
Merge two sorted arrays in-place from the end to avoid overwriting elements using three pointers.
March 19, 2026 Read →
Find the maximum number of consecutive 1s achievable by flipping at most k zeros, using a variable sliding window.
March 19, 2026 Read →
Find the longest substring without duplicate characters using a HashSet-backed shrinkable sliding window.
March 19, 2026 Read →
Find the longest substring with at most k character replacements by tracking the maximum frequency char in the window.
March 19, 2026 Read →
Check if any permutation of s1 is a substring of s2 using a fixed-size sliding window with character frequency comparison.
March 19, 2026 Read →
Maximize water trapped between two vertical lines using inward two pointers that always move the shorter line inward.
March 19, 2026 Read →
Find all unique triplets summing to zero by sorting and using two pointers for each fixed element with careful duplicate skipping.
March 19, 2026 Read →
Find two numbers summing to target in a 1-indexed sorted array using inward two pointers in O(n) time O(1) space.
March 19, 2026 Read →
Find k closest integers to x in a sorted array using binary search to locate the optimal left boundary of the window.
March 19, 2026 Read →
Find the maximum consecutive ones in a binary array when you can flip at most k zeros, identical to the Max Consecutive Ones III pattern.
March 19, 2026 Read →
Find the smallest window in s containing all characters of t using a have/need counter to track when the window is valid.
March 19, 2026 Read →
Find the maximum in every sliding window of size k in O(n) using a monotonic decreasing deque of indices.
March 19, 2026 Read →
Sort an array of 0s, 1s, and 2s in one pass using the Dutch National Flag 3-pointer algorithm.
March 19, 2026 Read →
Calculate trapped rainwater using optimal two-pointer approach that tracks left and right maximums without extra space.
March 19, 2026 Read →