How to use this page: These 25 problems are the most commonly asked easy-level DSA questions in coding interviews. They are ranked by frequency across Google, Amazon, Meta, Microsoft, and other top tech companies.
For each problem you will find the pattern it belongs to, which companies commonly ask it, a brief approach, a key insight, and the expected time/space complexity. Use the filter buttons to focus on a specific pattern.
Iterate through the array. For each number, compute the complement (target - num) and check if it already exists in a hash map. If it does, return both indices. Otherwise, store the current number and its index in the map.
O(n)
Space: O(n)
Use a stack. Push opening brackets onto the stack. When you encounter a closing bracket, check if the top of the stack is the matching opening bracket. If not, or if the stack is empty, return False. At the end, the stack must be empty.
O(n)
Space: O(n)
Create a dummy head node. Use two pointers to walk through both lists, always appending the smaller node to the merged list. When one list is exhausted, attach the remainder of the other list.
O(n + m)
Space: O(1)
Track the minimum price seen so far as you iterate. At each step, compute the profit if you sold today (current price minus minimum). Update the maximum profit accordingly. One pass is all you need.
O(n)
Space: O(1)
Use two pointers starting from both ends. Skip non-alphanumeric characters. Compare the characters at both pointers (case-insensitive). If they ever differ, return False. If the pointers meet, return True.
O(n)
Space: O(1)
Recursively swap the left and right children of every node. Base case: if the node is None, return None. Swap left and right, then recurse on both subtrees. Return the root.
O(n)
Space: O(h) where h is tree height
Count the frequency of each character in both strings using a hash map (or Counter). If the frequency maps are identical, the strings are anagrams. Alternatively, use a single counter: increment for string s, decrement for string t, then check all counts are zero.
O(n)
Space: O(1) (fixed 26-char alphabet)
Maintain two pointers, left and right, defining the search range. Compute mid. If nums[mid] equals target, return mid. If target is smaller, move right to mid - 1. If larger, move left to mid + 1. Repeat until left exceeds right.
O(log n)
Space: O(1)
Use Floyd's Tortoise and Hare algorithm. Move a slow pointer one step and a fast pointer two steps at a time. If they meet, there is a cycle. If the fast pointer reaches the end (None), there is no cycle.
O(n)
Space: O(1)
Use Kadane's algorithm. Maintain a running sum of the current subarray. If the running sum drops below zero, reset it to zero (start a new subarray). Track the maximum sum seen at any point.
O(n)
Space: O(1)
Iterate through the list with three pointers: prev, curr, and next. At each step, save curr.next, point curr.next to prev, then advance prev and curr forward. When curr is None, prev is the new head.
O(n)
Space: O(1)
Add each element to a set as you iterate. Before adding, check if it already exists in the set. If it does, return True immediately. If you finish the loop without a match, return False.
O(n)
Space: O(n)
Recursively compute the depth of the left and right subtrees. The depth of the current node is 1 + max(left_depth, right_depth). Base case: if the node is None, return 0.
O(n)
Space: O(h) where h is tree height
The number of ways to reach step n equals the sum of ways to reach step n-1 and step n-2 (since you can take 1 or 2 steps). This is the Fibonacci sequence. Use two variables to iterate bottom-up in O(1) space.
O(n)
Space: O(1)
Recursively compare both trees. If both nodes are None, return True. If one is None and the other is not, or their values differ, return False. Otherwise, recursively check left subtrees and right subtrees.
O(n)
Space: O(h) where h is tree height
Write a helper that takes two nodes and checks if they are mirror images. Two nodes are mirrors if both are None, or both have the same value AND left1 mirrors right2 AND right1 mirrors left2. Call this helper with root.left and root.right.
O(n)
Space: O(h) where h is tree height
XOR all elements together. Since a XOR a = 0 and a XOR 0 = a, all duplicate pairs cancel out, leaving only the single unique number.
O(n)
Space: O(1)
Use two pointers, one for each list. When a pointer reaches the end of its list, redirect it to the head of the other list. Both pointers will travel the same total distance and meet at the intersection node (or both reach None if no intersection).
O(n + m)
Space: O(1)
Use a slow pointer to track the position where the next non-zero element should go. Iterate with a fast pointer. When you find a non-zero, swap it with the element at the slow pointer and advance slow. All zeroes bubble to the end.
O(n)
Space: O(1)
Compute the expected sum of numbers 0 through n using the formula n*(n+1)/2. Subtract the actual sum of the array. The difference is the missing number. Alternatively, XOR all indices and all values — the missing number remains.
O(n)
Space: O(1)
Find the middle of the list using slow/fast pointers. Reverse the second half in-place. Compare the first half with the reversed second half node by node. If all values match, it is a palindrome.
O(n)
Space: O(1)
Maintain two stacks: one for the values and one for the running minimum. On push, also push the new minimum (min of current value and current top of min stack). On pop, pop from both stacks. getMin peeks the min stack.
O(1) per operation
Space: O(n)
Use a recursive depth function. At each node, the diameter through that node is left_depth + right_depth. Track the global maximum diameter as you recurse. Return the depth (1 + max of children) for use by parent calls.
O(n)
Space: O(h) where h is tree height
For each node in the main tree, check if the subtree rooted there is identical to the target tree (using the Same Tree check). If any node matches, return True. Recurse on left and right children.
O(n * m)
Space: O(h) where h is tree height
Use two pointers at both ends of the array. Compare absolute values: the larger absolute value's square goes at the end of the result array. Move that pointer inward. Fill the result array from right to left.
O(n)
Space: O(n)
You know the problems.
Now master the patterns.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →