
1. Two Sum (Easy - Array Problem)
Question: Given an array of integers `nums` and an integer `target`, return the indices of the two numbers that add up to the target. Assume each input has exactly one solution, and you cannot use the same element twice.
Answer: The brute-force approach uses nested loops (O(n²)), but a hash map optimizes this. Iterate once, storing each number and its index. For each number, check if its complement (target - current) exists in the map. If found, return the indices; otherwise, add the current number and index. This leverages O(n) time and O(n) space, making it efficient for FAANG interviews.
Code:
function twoSum(nums, target) { const numMap = new Map(); for (let i = 0; i < nums.length; i++) { const complement = target - nums[i]; if (numMap.has(complement)) { return [numMap.get(complement), i]; } numMap.set(nums[i], i); } return []; // No solution found }
Complexity: Time: O(n), Space: O(n). Application: Used in recommendation systems to find pairs. Tips: Practice edge cases (e.g., negative numbers, duplicates).
2. Invert Binary Tree (Medium - Tree Problem)
Question: Given the root of a binary tree, invert it by swapping the left and right children of each node recursively or iteratively.
Answer: Use a recursive depth-first approach. For each node, swap its left and right subtrees before recursing. The base case is a null node. Alternatively, use a queue for BFS. This is a favorite at Google to test recursion and tree traversal.
Code:
function invertTree(root) { if (!root) return null; [root.left, root.right] = [invertTree(root.right), invertTree(root.left)]; return root; }
Complexity: Time: O(n), Space: O(h) where h is height (due to recursion stack). Application: Useful in image processing or tree mirroring. Tips: Understand both recursive and iterative solutions.
3. Longest Substring Without Repeating Characters (Medium - String Problem)
Question: Given a string, find the length of the longest substring without repeating characters.
Answer: Implement a sliding window with a hash set. Maintain a window [left, right], adding characters and sliding left when duplicates are found. Track the maximum length. This is a staple at Amazon for string manipulation skills.
Code:
function lengthOfLongestSubstring(s) { const charSet = new Set(); let maxLength = 0, left = 0; for (let right = 0; right < s.length; right++) { while (charSet.has(s[right])) charSet.delete(s[left++]); charSet.add(s[right]); maxLength = Math.max(maxLength, right - left + 1); } return maxLength; }
Complexity: Time: O(n), Space: O(min(m, n)) where m is char set size. Application: Text analysis, plagiarism detection. Tips: Test with empty strings and single characters.
4. Merge Intervals (Medium - Array Problem)
Question: Given a list of intervals, merge all overlapping intervals into a single interval.
Answer: Sort intervals by start time, then iterate. If the current interval overlaps (start ≤ previous end), merge by updating the end; otherwise, add the interval. This is a classic Apple interview question for array manipulation.
Code:
function mergeIntervals(intervals) { intervals.sort((a, b) => a[0] - b[0]); const merged = []; for (let interval of intervals) { if (!merged.length || merged[merged.length - 1][1] < interval[0]) { merged.push(interval); } else { merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1]); } } return merged; }
Complexity: Time: O(n log n), Space: O(1) or O(n) for output. Application: Scheduling, resource allocation. Tips: Handle unsorted or empty inputs.
5. LRU Cache (Hard - Design Problem)
Question: Design a Least Recently Used (LRU) cache with get (O(1)) and put (O(1)) operations, supporting capacity limits.
Answer: Combine a hash map for O(1) access and a doubly linked list for order tracking. On get/put, move accessed/added nodes to the front (most recent), removing the least recent if over capacity. A Netflix favorite for system design.
Code:
class LRUCache { constructor(capacity) { this.capacity = capacity; this.cache = new Map(); } get(key) { if (!this.cache.has(key)) return -1; const value = this.cache.get(key); this.cache.delete(key); this.cache.set(key, value); return value; } put(key, value) { if (this.cache.has(key)) this.cache.delete(key); else if (this.cache.size >= this.capacity) { this.cache.delete(this.cache.keys().next().value); } this.cache.set(key, value); } }
Complexity: Time: O(1), Space: O(capacity). Application: Memory management, caching systems. Tips: Implement a full doubly linked list version for deeper understanding.
6. Top K Frequent Elements (Medium - Heap Problem)
Question: Given an array, find the k most frequent elements.
Answer: Use a hash map to count frequencies, then a min-heap of size k to track top elements. Add elements, removing the least frequent if over k. Meta often tests heap-based solutions.
Code:
function topKFrequent(nums, k) { const freqMap = new Map(); for (let num of nums) freqMap.set(num, (freqMap.get(num) || 0) + 1); return [...freqMap.entries()].sort((a, b) => b[1] - a[1]).slice(0, k).map(([num]) => num); }
Complexity: Time: O(n log k), Space: O(n). Application: Trend analysis, search engines. Tips: Use a priority queue for optimal heap implementation.
7. Valid Parentheses (Easy - Stack Problem)
Question: Given a string containing just the characters '(', ')', '{', '}', '[', ']', determine if the input string is valid (properly nested).
Answer: Use a stack to track opening brackets. Push on open, pop and match on close. If unmatched or stack remains, invalid. A Google classic for stack mastery.
Code:
function isValid(s) { const stack = []; const bracketMap = { '(': ')', '{': '}', '[': ']' }; for (let char of s) { if (bracketMap[char]) stack.push(char); else if (char !== bracketMap[stack.pop()]) return false; } return stack.length === 0; }
Complexity: Time: O(n), Space: O(n). Application: Syntax validation, compilers. Tips: Test with mixed brackets and odd lengths.
8. Maximum Path Sum in Binary Tree (Hard - Dynamic Programming)
Question: Find the maximum path sum in a binary tree where the path can start and end at any node.
Answer: Use a recursive DP approach. For each node, compute the max path sum including or excluding it, updating a global max. Handle negative values by taking 0 if negative. An Amazon favorite for DP skills.
Code:
let maxPathSumValue = -Infinity; function maxPathSum(root) { maxGain(root); return maxPathSumValue; } function maxGain(node) { if (!node) return 0; const leftGain = Math.max(maxGain(node.left), 0); const rightGain = Math.max(maxGain(node.right), 0); const currentPath = node.val + leftGain + rightGain; maxPathSumValue = Math.max(maxPathSumValue, currentPath); return node.val + Math.max(leftGain, rightGain); }
Complexity: Time: O(n), Space: O(h). Application: Network optimization. Tips: Visualize tree paths to understand DP transitions.
9. Course Schedule (Medium - Graph Problem)
Question: Determine if it’s possible to finish all courses given prerequisites (directed graph).
Answer: Use DFS or Kahn’s algorithm to detect cycles. Build an adjacency list and indegree array, then process nodes with no prerequisites. Time: O(V + E), Space: O(V). Common at Facebook.
Code:
function canFinish(numCourses, prerequisites) { const adj = new Array(numCourses).fill().map(() => []); const indegree = new Array(numCourses).fill(0); for (let [course, pre] of prerequisites) { adj[pre].push(course); indegree[course]++; } const queue = []; for (let i = 0; i < numCourses; i++) if (indegree[i] === 0) queue.push(i); let count = 0; while (queue.length) { const course = queue.shift(); count++; for (let next of adj[course]) { if (--indegree[next] === 0) queue.push(next); } } return count === numCourses; }
Complexity: Time: O(V + E), Space: O(V). Application: Dependency resolution. Tips: Practice topological sort variations.
10. Word Break (Hard - Dynamic Programming)
Question: Given a string and a dictionary, determine if it can be segmented into dictionary words.
Answer: Use a DP array where dp[i] is true if substring [0, i) is segmentable. Check all prefixes against the dictionary. Time: O(n²), Space: O(n). A Netflix challenge for DP proficiency.
Code:
function wordBreak(s, wordDict) { const dp = new Array(s.length + 1).fill(false); dp[0] = true; for (let i = 1; i <= s.length; i++) { for (let word of wordDict) { if (word.length <= i && dp[i - word.length] && s.substr(i - word.length, word.length) === word) { dp[i] = true; break; } } } return dp[s.length]; }
Complexity: Time: O(n²), Space: O(n). Application: Text parsing, NLP. Tips: Optimize with a Trie for large dictionaries.
Why These Questions Matter in 2025
FAANG interviews in 2025 focus on scalability, real-world problem-solving, and advanced algorithms. These questions span core data structures and techniques, reflecting challenges in cloud computing, AI, and big data. Mastering them demonstrates your ability to handle complex systems, a key requirement as companies scale globally.
Conclusion
Preparing for FAANG coding interviews requires a deep understanding of problems like Two Sum, LRU Cache, and Word Break. Practice on LeetCode, analyze time/space trade-offs, and simulate interviews. With consistent effort, these skills will unlock opportunities at FAANG companies in 2025!