Skip to main contentOpteroAIBeta

Data Structures & Algorithms interview questions

Classic data structures and algorithms interview questions. These test your problem-solving ability and are common at companies that emphasize coding interviews.

12 questions
4 easy6 medium2 hard

1.Given a sorted array, find two numbers that add up to a target sum.

easy
How to approach thisUse the two-pointer technique: start with one pointer at the beginning and one at the end. If the sum is too small, advance the left pointer. If too large, retreat the right pointer. This runs in O(n) time and O(1) space, which is better than a hash map approach for sorted input.

2.How would you detect a cycle in a linked list?

medium
How to approach thisUse Floyd's tortoise and hare algorithm: one pointer moves one step at a time, the other moves two steps. If they meet, there is a cycle. To find the cycle start, reset one pointer to the head and advance both one step at a time; they meet at the cycle entry. O(n) time, O(1) space.

3.Implement an LRU cache with O(1) get and put operations.

hard
How to approach thisCombine a hash map (for O(1) key lookup) with a doubly linked list (for O(1) insertion and removal at both ends). On get: move the node to the head. On put: insert at head, evict from tail if over capacity. Each hash map entry points to the corresponding list node.

4.Explain the time complexity of operations on a hash map. What happens during a hash collision?

easy
How to approach thisAverage case: O(1) for get, put, and delete. Worst case: O(n) if all keys hash to the same bucket. Collisions are handled by chaining (linked list per bucket) or open addressing (probing for the next empty slot). Good hash functions and load factor management keep performance near O(1).

5.How would you find the kth largest element in an unsorted array?

medium
How to approach thisThree approaches: sort and index (O(n log n)), use a min-heap of size k (O(n log k)), or use Quickselect (O(n) average, O(n^2) worst). Quickselect partitions the array like QuickSort but only recurses into the relevant half. For interviews, know all three and discuss trade-offs.

6.Explain when you would use a stack vs. a queue.

easy
How to approach thisStack (LIFO): undo operations, expression evaluation, DFS traversal, parentheses matching. Queue (FIFO): BFS traversal, task scheduling, message processing, breadth-first printing. A deque (double-ended queue) supports both patterns. Knowing which data structure matches the problem is half the interview answer.

7.How would you serialize and deserialize a binary tree?

hard
How to approach thisUse preorder traversal, encoding null children as a sentinel value (e.g., '#'). Serialize: visit root, serialize left, serialize right, recording values and nulls. Deserialize: read values sequentially, building nodes recursively, consuming a null marker when a subtree is empty. BFS (level-order) also works well.

8.What is dynamic programming, and how do you decide when to use it?

medium
How to approach thisDP applies when a problem has overlapping subproblems and optimal substructure. If you find yourself solving the same subproblem multiple times in a recursive solution, DP can help. Start with a recursive solution, add memoization (top-down), or identify the recurrence relation and build a table (bottom-up). Classic examples: Fibonacci, coin change, longest common subsequence.

9.Implement a function to check if a binary tree is balanced.

medium
How to approach thisA balanced tree has subtrees whose heights differ by at most 1 at every node. Use a recursive approach that returns the height at each node. If any subtree is unbalanced, short-circuit by returning -1. This runs in O(n) time with a single pass, compared to O(n^2) if you compute heights separately at each level.

10.How does a trie differ from a hash map for string lookups?

medium
How to approach thisA hash map has O(1) average lookup for exact keys but cannot do prefix searches. A trie (prefix tree) supports efficient prefix queries (autocomplete), lexicographic ordering, and shared prefix storage. Tries use more memory due to pointers. Use a hash map for exact lookup, a trie when you need prefix matching or sorted iteration.

11.Explain the difference between BFS and DFS. When would you use each?

easy
How to approach thisBFS explores level by level (uses a queue), finding the shortest path in unweighted graphs. DFS explores as deep as possible first (uses a stack/recursion). Use BFS for shortest path, level-order traversal, and when the solution is close to the root. Use DFS for topological sort, cycle detection, and when you need to explore all paths.

12.Given a string, find the length of the longest substring without repeating characters.

medium
How to approach thisUse the sliding window technique: maintain a window [left, right] and a set of characters in the window. Expand right, adding characters. When a duplicate is found, shrink left until the duplicate is removed. Track the maximum window size. O(n) time, O(min(n, alphabet_size)) space.

Prepare further

More interview topics