Algorithms & Data Structures
Master interview algorithms: time/space complexity, common patterns (sliding window, two pointers, BFS/DFS, DP), and problem-solving approach.
Technical interviews at top tech companies focus on algorithms and data structures - time/space complexity analysis, pattern recognition, and clean implementation. The key is systematic problem-solving, not memorization.
Learn patterns, not solutions. Understand why each pattern works (invariants). Always analyze time and space complexity. Communicate your approach before coding. Test with edge cases. Practice with spaced repetition rather than grinding.
Workflow
- Analyze time and space complexityBig O notation is the language of algorithm analysis - required for every interview discussion.
- Apply sliding window and two pointersThese patterns reduce O(n²) brute-force approaches on arrays/strings to O(n).
- Traverse graphs and trees with BFS/DFSBFS finds shortest paths; DFS explores all paths. Tree/graph problems appear in ~30% of interviews.
- Solve dynamic programming problemsDP is the pattern for optimization problems with overlapping subproblems.
- Use a systematic interview approachInterviewers evaluate problem-solving process, not just the final answer.
Output Format
Big O for every solution, sliding window/two-pointer for array problems, BFS for shortest path, DFS for existence/cycle, DP for optimization, systematic clarify-example-approach-code-test interview flow.
Examples
Two Sum (hash map): `function twoSum(nums: number[], target: number): [number,number] { const seen = new Map<number,number>(); for (let i = 0; i < nums.length; i++) { const complement = target - nums[i]; if (seen.has(complement)) return [seen.get(complement)!, i]; seen.set(nums[i], i) }; throw new Error("No solution") } // O(n) time, O(n) space`Coin change (DP): `function coinChange(coins: number[], amount: number): number { const dp = new Array(amount + 1).fill(Infinity); dp[0] = 0; for (let i = 1; i <= amount; i++) { for (const coin of coins) { if (coin <= i) dp[i] = Math.min(dp[i], dp[i - coin] + 1) } }; return dp[amount] === Infinity ? -1 : dp[amount] } // O(amount * coins) time`Binary search: `function binarySearch(arr: number[], target: number): number { let l = 0, r = arr.length - 1; while (l <= r) { const mid = l + Math.floor((r - l) / 2); if (arr[mid] === target) return mid; else if (arr[mid] < target) l = mid + 1; else r = mid - 1 }; return -1 } // O(log n)`Edge Cases & Gotchas
- Integer overflow: `mid = (l + r) / 2` can overflow for large arrays. Use `mid = l + Math.floor((r - l) / 2)` instead (JavaScript is less susceptible but good habit)
- Graph with cycles: always use a `visited` set in DFS/BFS on graphs (not trees) to prevent infinite loops
- DP space optimization: 2D DP tables can often be compressed to 1D if only the previous row is needed (e.g., knapsack). Reduces space from O(n²) to O(n)
When to Break These Rules
- Interview algorithms rarely map directly to production code. In production, reach for library implementations (sort, pathfinding) rather than rolling your own - correctness and maintainability matter more than algorithmic cleverness.
Reference Files
LeetCode: https://leetcode.com/NeetCode: https://neetcode.io/CLRS (book): Introduction to AlgorithmsPatterns: https://patterns.eecs.berkeley.edu/