Company Practice
Step 22 in the DSA path · 2 concepts · 235 problems
📘 Learn Company Practice from zero
These are all "easy" tier, but FAANG graders judge how cleanly you reason, not whether you eventually pass. Use one disciplined loop on every problem:
- Read & restate. Say the problem back in your own words and confirm the output type (boolean? array? rebuilt tree?). Surfaces misreads before you waste code.
- Pin the constraints. Are values bounded (lowercase letters →
int[26]; values 0–1000 → counting sort)? Is n small or up to 10⁵? Constraints are the hint — a bounded range points to counting; "k operations" points to a heap. - Brute force out loud first. State the obvious O(n²) or simulate-everything solution. It proves you understand the problem and gives a correctness baseline. Never go silent hunting for the clever trick.
- Spot the pattern. Map a signal to a technique using the framework above — frequency→hash map, sorted-by-rank→bucket, pick-max-repeatedly→heap, tree-compare→DFS. Double-check the mapping: a "subtract" or "max" keyword does not automatically mean heap.
- Optimize. Replace the bottleneck: a nested membership check becomes a
HashSet; a sort-then-scan becomes counting sort when the range is small. - Edge cases. Empty input, single element, all-equal, negatives, duplicates, one-node tree, value not present.
- Test by hand. Trace the given example, then one nasty case. State final time/space complexity unprompted.
Talk through every step. A calm, structured walk on an easy problem signals exactly the engineer they want to hire.
✨ Added by the guide to build intuition — not from the source course.
🎯 Guided practice
Walk-through: Relative Sort Array — sort arr1 so elements appear in the order they occur in arr2; anything not in arr2 goes at the end in ascending order.
- Restate: output is a permutation of
arr1, partially ordered by an external reference list, with leftovers sorted normally. - Brute force: a custom comparator — "rank by index in
arr2(via a value→rank map), else sort those leftovers by value, placing them after the ranked ones." Correct and O(n log n). Worth stating as a fallback. - Spot the signal: the prompt bounds values to 0–1000. "Sorted output + small bounded range" is the textbook trigger for counting sort, which beats the comparator's O(n log n) with O(n + K).
- Why counting sort wins here: no comparisons needed. Count every value in
arr1intocount[0..1000]. Then walkarr2in order, emitting each valuecount[v]times and zeroing it. Finally sweepcount0→1000 to append the untouched leftovers, already ascending for free.
Optimal outline:
- Build
countarray overarr1(one pass). - For each
vinarr2: appendvexactlycount[v]times, then setcount[v]=0. - For
vfrom 0 to 1000: appendvexactlycount[v]times — these are the not-in-arr2leftovers, emitted in sorted order automatically.
Complexity: O(n + m + K) time, O(K) extra space (n = arr1 length, m = arr2 length, K = value range, here 1001). Edge cases: values in arr1 not in arr2, duplicates in both, arr2 a strict subset of arr1's distinct values. The lesson: let the constraint (bounded values) pick the technique — that recognition is the whole game.
✨ Added by the guide — work these before the full problem set.
Lessons in this topic
- ○Take Gifts From the Richest Pileeasy
- ○Coin Change
- ○easy Redistribute Characters to Make All Strings Equal
- ○easy Add Binary
- ○easy Find Pivot Index
- ○easy Toeplitz Matrix
- ○easy Buddy Strings
- ○easy Longest Common Prefix
- ○easy Valid Mountain Array
- ○easy Isomorphic Strings
- ○easy Remove All Adjacent Duplicates In String
- ○easy Can Place Flowers
- ○easy Make Array Zero by Subtracting Equal Amounts
- ○easy Set Mismatch
- ○easy Second Minimum Node In a Binary Tree
- ○easy Minimum Depth of a Binary Tree
- ○easy Reverse a LinkedList
- ○easy Leaf-Similar Trees
- ○easy Is Subsequence
- ○easy Minimum Time to Type Word Using Special Typewriter
- ○easy Tree Diameter
- ○easy Maximum Number of Balloons
- ○easy Path Crossing
- ○easy Length of Last Word
- ○easy Intersection of Two Arrays
- ○easy Island Perimeter
- ○easy Binary Tree Path Sum
- ○easy Special Array With X Elements Greater Than or Equal X
- ○easy Search Insert Position
- ○easy Can Place Flowers
- ○easy Moving Average from Data Stream
- ○easy Pair with Target Sum
- ○easy Relative Sort Array
- ○easy Repeated Substring Pattern
- ○easy Valid Perfect Square
- ○easy Move Zeroes
- ○easy Buy Two Chocolates
- ○easy Transpose Matrix
- ○easy Monotonic Array
- ○easy Maximum Depth or Height of Binary Tree
- ○easy Missing Ranges
- ○easy Root Equals Sum of Children
- ○easy Find Common Characters
- ○easy Count Nodes Equal to Average of Subtree
- ○easy Sqrt
- ○easy Palindrome Permutation
- ○easy Excel Sheet Column Title
- ○easy Fizz Buzz
- ○easy Find the Difference
- ○easy Valid Palindrome
- ○easy Rank Transform of an Array
- ○easy Merge Strings Alternately
- ○easy Increasing Order Search Tree
- ○easy How Many Numbers Are Smaller Than the Current Number
- ○easy Min Cost Climbing Stairs
- ○easy Largest Odd Number in String
- ○easy Binary Tree Paths
- ○easy Plus One
- ○easy Best Time to Buy and Sell
- ○easy Find the Difference of Two Arrays
- ○easy Intersection of Two Arrays
- ○easy Merge Strings Alternately
- ○easy Find the Index of the First Occurrence in a String
- ○easy Remove Duplicates from Sorted List
- ○easy Reverse a LinkedList
- ○easy Merge Two Sorted Lists
- ○easy Number of Islands
- ○medium Rotate List
- ○medium Diagonal Traverse
- ○medium Find Leaves of Binary Tree
- ○medium Shortest Path in Binary Matrix
- ○medium Valid Triangle Number
- ○medium Maximum Swap
- ○medium Valid Parenthesis String
- ○medium Custom Sort String
- ○medium Edit Distance
- ○medium Binary Tree Vertical Order Traversal
- ○medium Maximum Length of Subarray With Positive Product
- ○medium Gray Code
- ○medium Remove All Ones With Row and Column Flips II
- ○medium Buildings With an Ocean View
- ○medium Nth Digit
- ○medium Largest Submatrix With Rearrangements
- ○medium 2 Keys Keyboard
- ○medium Reverse Integer
- ○medium Basic Calculator II
- ○medium Restore IP Addresses
- ○medium Count the Number of Good Subarrays
- ○medium Arithmetic Slices
- ○medium Triplet Sum to Zero
- ○medium Nested List Weight Sum
- ○medium Find Original Array From Doubled Array
- ○medium Minimum Suffix Flips
- ○medium Unique Length-3 Palindromic Subsequences
- ○medium Longest Substring Without Repeating Characters
- ○medium Dot Product of Two Sparse Vectors
- ○medium Reverse Words in a String II
- ○medium Minimum Number of Operations to Make Array Empty
- ○medium Minimum Difference Between Largest and Smallest Value in Three Moves
- ○medium Decode Ways
- ○medium Max Consecutive Ones III
- ○medium Longest Arithmetic Subsequence
- ○medium Next Greater Element II
- ○medium Letter Combinations of a Phone Number
- ○medium Quadruple Sum to Target
- ○medium Subarray Sum Equals K
- ○medium Matchsticks to Square
- ○medium Make Sum Divisible by P
- ○medium 01 Matrix
- ○medium Next Permutation
- ○medium Next Permutation
- ○medium Score of Parentheses
- ○medium Rotting Oranges
- ○medium Maximum Points You Can Obtain from Cards
- ○medium Search in Rotated Array
- ○medium Simplify Path
- ○medium Maximum Length of a Concatenated String with Unique Characters
- ○medium Minimum Deletions to Make Character Frequencies Unique
- ○medium Strobogrammatic Number II
- ○medium Word Search
- ○medium Lowest Common Ancestor of a Binary Search Tree
- ○medium Inorder Successor in BST
- ○medium Minimum Adjacent Swaps to Make a Valid Array
- ○medium Maximum Length of Semi-Decreasing Subarrays
- ○medium Remove Nth Node From End of List
- ○medium 'K' Closest Points to the Origin
- ○medium Distribute Coins in Binary Tree
- ○medium Reverse Integer
- ○medium Minimum Area Rectangle
- ○medium House Robber II
- ○medium Sum of Path Numbers
- ○medium Spiral Matrix
- ○medium Maximize Distance to Closest Person
- ○medium Count Unreachable Pairs of Nodes in an Undirected Graph
- ○medium Merge Intervals
- ○medium Delete Node in a BST
- ○medium Split Linked List in Parts
- ○medium Single Number II
- ○medium Design Add and Search Words Data Structure
- ○medium Right View of a Binary Tree
- ○medium Sum of Subarray Minimums
- ○medium Maximum Difference Between Node and Ancestor
- ○medium 4Sum II
- ○medium Merge Intervals
- ○medium Number of Islands
- ○medium Rotate Image
- ○medium Deepest Leaves Sum
- ○medium Furthest Building You Can Reach
- ○medium Clone Graph
- ○medium Combinations
- ○medium The kth Factor of n
- ○medium Minimizing Array After Replacing Pairs With Their Product
- ○medium Longest Palindromic Subsequence
- ○medium Missing Element in Sorted Array
- ○medium Maximum Level Sum of a Binary Tree
- ○medium Coin Change II
- ○medium Fair Distribution of Cookies
- ○medium Kth Smallest Number
- ○medium Number of Substrings Containing All Three Characters
- ○medium Multiply Strings
- ○medium Sum of Subarray Minimums
- ○medium Find Leaves of Binary Tree
- ○medium Maximum Product Subarray
- ○medium Capacity To Ship Packages Within D Days
- ○medium Best Time to Buy and Sell Stock II
- ○medium Valid Sudoku
- ○medium Rotate Image
- ○medium Same Tree
- ○medium Count and Say
- ○medium Largest Palindromic Number
- ○medium Longest Increasing Subsequence
- ○medium Combination Sum
- ○medium Contiguous Array
- ○medium Remove Duplicate Letters
- ○medium Next Permutation
- ○medium Implement Trie Prefix Tree
- ○medium Next Permutation
- ○medium Subsets
- ○medium Number of Islands
- ○medium 132 Pattern
- ○medium Combination Sum
- ○medium Edit Distance
- ○medium Valid Sudoku
- ○medium Number of Provinces
- ○medium Container With Most Water
- ○medium Number of Islands
- ○medium Number of Islands
- ○medium Meeting Rooms II
- ○medium Buildings With an Ocean View
- ○hard Max Points on a Line
- ○hard Closest Binary Search Tree Value II
- ○hard Sliding Window Median
- ○hard Word Ladder
- ○hard Alien Dictionary
- ○hard Median of Two Sorted Arrays
- ○hard Minimum Window Substring
- ○hard Largest Rectangle in Histogram
- ○hard Best Meeting Point
- ○hard Serialize and Deserialize Binary Tree
- ○hard Maximum Sum of 3 Non-Overlapping Subarrays
- ○hard Constrained Subsequence Sum
- ○hard Shortest Subarray with Sum at Least K
- ○hard Making A Large Island
- ○hard Regular Expression Matching
- ○hard Number of Valid Subarrays
- ○hard Reverse Pairs
- ○hard Basic Calculator
- ○hard First Missing Positive
- ○hard Count Vowels Permutation
- ○hard Nth Magical Number
- ○hard Serialize and Deserialize Binary Tree
- ○hard Largest Rectangle in Histogram
- ○hard Concatenated Words
- ○hard Candy
- ○hard Longest Increasing Path in a Matrix
- ○hard Sliding Window Maximum
- ○hard Minimum Replacements to Sort the Array
- ○hard Arithmetic Slices II - Subsequence
- ○hard Valid Palindrome III
- ○hard Count Unique Characters of All Substrings of a Given String
- ○hard Count All Valid Pickup and Delivery Options
- ○hard Burst Balloons
- ○hard Word Ladder
- ○hard Length of the Longest Valid Substring
- ○hard Sum of Distances in Tree
- ○hard Word Ladder II
- ○hard Count Array Pairs Divisible by K
- ○hard Maximum Score of a Good Subarray
- ○hard Number of Flowers in Full Bloom
- ○hard Word Ladder
- ○hard Trapping Rain Water
- ○hard Median of Two Sorted Arrays
- ○hard Path with Maximum Sum
- ○hard First Missing Positive
- ○hard Longest Valid Parentheses
- ○Solution Take Gifts From the Richest Pile
- ○Solution Coin Change