Arrays-strings

40 articles

dsa12 min read

Contains Duplicate (LeetCode 217) — Hash Set in O(n)

LeetCode 217 asks whether any value repeats in an array. The hash set answer runs in O(n) time and O(n) space. This guide compares it against sorting and brute force, shows why hash sets degrade to O(n) in the worst case, and works through the Contains Duplicate II and III follow-ups interviewers actually ask next.

Read →
dsa4 min read

Plus One — Carry Propagation in Arrays

LeetCode 66 — increment a large integer represented as a digit array, propagating the carry. The deceptively simple FAANG warmup that catches careless coders.

Read →
dsa5 min read

4Sum — LC 18 Two Loops Plus Two Pointers

LeetCode 18 generalizes 3Sum to four numbers with target sum. Sort, fix two indices, and run two pointers — O(n^3) time and clean duplicate handling.

Read →
dsa6 min read

Gas Station — Greedy One-Pass Circuit Feasibility [LC 134]

LeetCode 134 — asked at Amazon, Google, and Microsoft. Find the unique valid starting station in a circular gas route using a two-insight greedy: global feasibility check plus a local reset that eliminates O(n) candidates at once, giving O(n) time and O(1) space.

Read →
dsa6 min read

Daily Temperatures — Monotonic Stack Next Greater Element [LC 739]

LeetCode 739 — asked at Amazon, Google, and Meta. Find the number of days until a warmer temperature using a monotonic decreasing stack. Store indices not temperatures, pop when current day is warmer, and solve six related problems with the same O(n) pattern.

Read →
dsa6 min read

Partition Labels — Greedy Last-Occurrence Interval Merging [LC 763]

LeetCode 763 — asked at Amazon, Google, and Meta. Partition a string into the maximum number of pieces so each letter appears in exactly one piece. Map each character to its last occurrence, then greedily extend the current partition boundary — O(n) time, O(1) space.

Read →
dsa5 min read

Task Scheduler — Greedy Formula and Max-Heap Simulation [LC 621]

LeetCode 621 — asked at Google, Meta, and Amazon. Schedule tasks with a cooldown n to minimize total time. Use the greedy formula max(len(tasks), (max_count - 1) * (n + 1) + count_of_max) or simulate with a max-heap and queue — both O(tasks) time.

Read →
dsa5 min read

Minimum Arrows to Burst Balloons — Greedy Interval Scheduling [LC 452]

LeetCode 452 — asked at Amazon, Google, and Microsoft. Find the minimum number of arrows to burst all balloons by sorting by end coordinate and greedily shooting through overlapping intervals. O(n log n) time, O(1) space — the classic greedy interval scheduling pattern.

Read →
dsa6 min read

Maximum Swap — Greedy Last-Occurrence Digit Tracking [LC 670]

LeetCode 670 — asked at Meta and Amazon. Given a non-negative integer, swap at most one pair of digits to get the maximum value. Track the last occurrence of each digit, then greedily find the leftmost position where a larger digit appears later — O(n) time, O(1) space.

Read →
dsa5 min read

Find Peak Element — Binary Search on Slope O(log n) [LC 162]

LeetCode 162 — asked at Google, Meta, and Amazon. Find any peak element in O(log n) using binary search on slope direction. If nums[mid] < nums[mid+1], a peak must exist in the right half — guaranteed by virtual negative infinity at both boundaries.

Read →
dsa5 min read

Missing Number — Gauss Formula and XOR Trick [LC 268]

LeetCode 268 — asked at Amazon, Microsoft, and Google. Find the missing number from 0 to n using the Gauss sum formula in O(n) time and O(1) space. Alternatively use XOR for a bit-manipulation approach. Both are classic array interview questions at FAANG companies.

Read →
dsa6 min read

Majority Element II — Extended Boyer-Moore Voting [LC 229]

LeetCode 229 — asked at Amazon, Google, and Microsoft. Find all elements appearing more than n/3 times using the Extended Boyer-Moore Voting Algorithm with two candidates. At most two such elements can exist — verify both candidates with a second pass. O(n) time, O(1) space.

Read →
dsa6 min read

Wiggle Sort II — O(n) Virtual Index Rearrangement [LC 324]

LeetCode 324 — asked at Google and Amazon. Rearrange an array so nums[0] < nums[1] > nums[2] < nums[3]... The key insight: find the median, then use a virtual index mapping to interleave smaller and larger halves without adjacent equal elements. O(n) time with nth_element.

Read →
dsa5 min read

Summary Ranges — Linear Scan Two-Pointer [LC 228]

LeetCode 228 — asked at Amazon, Google, and Microsoft. Collapse a sorted unique integer array into the smallest list of ranges. Use a two-pointer linear scan to detect where consecutive runs break. O(n) time, O(1) space — clean and fast.

Read →
dsa5 min read

Candy — Two-Pass Greedy Rating Satisfaction [LC 135]

LeetCode 135 — asked at Amazon, Google, and Microsoft. Give each child the minimum candies so higher-rated neighbors get more. Two greedy passes: left-to-right for left-neighbor constraint, right-to-left for right-neighbor constraint. O(n) time, O(n) space.

Read →
dsa5 min read

Minimum Moves to Equal Array Elements II — Why the Median Wins [LC 462]

LeetCode 462 — asked at Amazon, Meta, and Google. Find the minimum number of moves to equalize all array elements where each move increments or decrements one element by 1. The optimal target is the median — provable by absolute deviation minimization. O(n log n) time.

Read →
dsa5 min read

Array Nesting — Cycle Detection in Functional Graphs [LC 565]

LeetCode 565 — asked at Amazon and Google. Find the longest cycle in a functional graph where each index maps to nums[index]. Mark visited nodes in-place (set to n) to avoid revisiting. O(n) time, O(1) extra space — classic cycle detection pattern.

Read →
dsa6 min read

Advantage Shuffle — Greedy Sun Tzu Strategy [LC 870]

LeetCode 870 — asked at Google and Amazon. Rearrange array A to maximize the count of positions where A[i] > B[i]. Apply Sun Tzu greedy: against each of B's strongest, send your weakest if you cannot win; otherwise send your smallest winning element. O(n log n) time.

Read →
dsa6 min read

Subsets II — Backtracking with Duplicate Skip [LC 90]

LeetCode 90 — asked at Amazon, Apple, and Google. Generate all unique subsets from an array with duplicates. Sort first, then in backtracking skip duplicate elements at the same recursion depth. O(2^n) time — the cleanest duplicate-handling pattern in all backtracking problems.

Read →
dsa5 min read

Rotate String — The Concatenation Trick [LC 796]

LeetCode 796 — asked at Amazon and Google. Check if string s can become string goal by rotating. The elegant O(n) trick: concatenate s with itself and check if goal is a substring. One line of code once you see the insight.

Read →
dsa6 min read

Trapping Rain Water — Two-Pointer O(n) O(1) [LC 42]

LeetCode 42 — one of the most asked hard problems at Amazon, Google, Microsoft, and Meta. Compute trapped rainwater using two pointers in O(n) time and O(1) space. The key: water at any position is min(left_max, right_max) minus the height. Move the pointer with the smaller max inward.

Read →
dsa6 min read

Sliding Window Maximum — Monotonic Deque O(n) [LC 239]

LeetCode 239 — asked at Amazon, Google, and Microsoft. Find the maximum in each sliding window of size k in O(n) using a monotonic decreasing deque. The deque stores indices in decreasing order of value — pop from front when out of window, pop from back when current element is larger.

Read →
dsa6 min read

First Missing Positive — Cyclic Sort Index Marking [LC 41]

LeetCode 41 — asked at Amazon, Google, and Microsoft. Find the smallest missing positive integer in O(n) time and O(1) space. Use the array itself as a hash map: place each number i at index i-1, then scan for the first mismatch. The answer must be in [1, n+1].

Read →
dsa6 min read

Largest Rectangle in Histogram — Monotonic Stack O(n) [LC 84]

LeetCode 84 — asked at Amazon, Google, and Microsoft. Find the largest rectangle in a histogram using a monotonic increasing stack. For each bar, find the nearest shorter bars on both sides to compute the maximum width. O(n) time, O(n) space.

Read →