Skip to content

PUBLIC QUESTION PREVIEW

Data Structures & Algorithms interview questions

Arrays, trees, graphs, DP, sorting — the backbone of every technical interview.

Showing 12 of 60 source questions. Sample answers are study material, not employer-specific hiring guidance.

How to work through this preview

Attempt each question before reading its answer. For a concept, give a definition and a concrete example. For code, state constraints, walk through a small input, and check complexity. For design, separate requirements from assumptions and explain failure handling. Use real situations for behavioral answers.

QUESTION 1 / easy / conceptual

What is the time complexity of searching in a hash map, and when can it degrade to O(n)?

Sample answer

Average case is O(1) for search, insert, and delete. It degrades to O(n) in the worst case when there are many hash collisions — all keys map to the same bucket, making it a linear scan. This can happen with a poor hash function or adversarial inputs. A good hash function and load factor management (resizing when load > 0.75) keeps it near O(1) amortized.

Hints to guide your next attempt

  • Think about hash collisions
  • What happens when all keys hash to the same bucket?

Source tags: hash-map / time-complexity / collisions

QUESTION 2 / easy / coding

Reverse a linked list in-place. What is the time and space complexity?

Sample answer

Use three pointers — prev (null), curr (head), next (null). While curr != null: 1. Save next = curr.next 2. Point curr.next = prev 3. Move prev = curr 4. Move curr = next Return prev as the new head. Time: O(n) — single pass. Space: O(1) — no extra data structures.

Hints to guide your next attempt

  • You need three pointers: prev, current, next
  • Update next before moving current forward

Source tags: linked-list / in-place / pointers

QUESTION 3 / easy / conceptual

Explain the difference between BFS and DFS. When would you prefer one over the other?

Sample answer

BFS (Breadth-First Search) uses a queue and explores level by level. DFS (Depth-First Search) uses a stack (or recursion) and goes deep before backtracking. Use BFS when: finding shortest path in unweighted graphs, level-order traversal, "nearest" problems. Use DFS when: detecting cycles, topological sort, maze/puzzle solving, when memory is constrained (BFS can use O(w) space where w is max width — DFS uses O(h) where h is height).

Hints to guide your next attempt

  • Think about the data structure each uses
  • Shortest path vs. exploring deep paths

Source tags: graph / bfs / dfs / traversal

QUESTION 4 / medium / conceptual

What is dynamic programming? Explain with the classic coin change problem.

Sample answer

Dynamic programming solves problems by breaking them into overlapping subproblems and storing results to avoid recomputation (memoization or tabulation). Coin Change (minimum coins to make amount N): - State: dp[i] = minimum coins to make amount i - Transition: dp[i] = min(dp[i - coin] + 1) for each coin ≤ i - Base: dp[0] = 0, dp[i] = infinity initially Bottom-up: fill dp[0..N]. Answer is dp[N]. Time: O(N × coins), Space: O(N).

Hints to guide your next attempt

  • DP = optimal substructure + overlapping subproblems
  • Build up from smaller amounts

Source tags: dynamic-programming / coin-change / memoization

QUESTION 5 / easy / coding

Given an array of integers, find two numbers that add up to a target sum. Optimise for O(n) time.

Sample answer

Use a hash map to store visited numbers. For each number x in array: complement = target - x if complement in map → return [complement, x] else → map[x] = index Time: O(n), Space: O(n). This beats the naive O(n²) brute force and the O(n log n) two-pointer approach (which requires sorting).

Hints to guide your next attempt

  • A hash map lets you look up complements in O(1)
  • For each number x, check if (target - x) exists

Source tags: array / hash-map / two-sum

QUESTION 6 / medium / conceptual

What is the difference between a min-heap and a max-heap? How is a heap used in Dijkstra's algorithm?

Sample answer

Min-heap: parent ≤ children. Root is always the minimum. Max-heap: parent ≥ children. Root is always the maximum. Both support insert and extract in O(log n). In Dijkstra's, we always process the unvisited node with the smallest known distance. A min-heap (priority queue) lets us extract that node in O(log V) instead of O(V) linear scan — bringing total complexity from O(V²) to O((V + E) log V).

Hints to guide your next attempt

  • Heap property: parent relationship with children
  • Dijkstra needs the next shortest unvisited node fast

Source tags: heap / priority-queue / dijkstra / graph

QUESTION 7 / medium / coding

Detect a cycle in a linked list. Explain Floyd's tortoise and hare algorithm.

Sample answer

Floyd's cycle detection: use two pointers — slow moves 1 step, fast moves 2 steps. If they ever point to the same node → cycle exists. If fast reaches null → no cycle. Why it works: in a cycle, fast "laps" slow. The relative speed is 1 node/step, so they meet within at most (cycle length) iterations. Time: O(n), Space: O(1) — no visited set needed.

Hints to guide your next attempt

  • Two pointers moving at different speeds
  • If there is a cycle, they must eventually meet

Source tags: linked-list / two-pointers / cycle-detection / floyd's

QUESTION 8 / medium / conceptual

What is a balanced BST? Why does balance matter, and how does an AVL tree maintain it?

Sample answer

A BST is balanced when the height difference between left and right subtrees is bounded, keeping operations O(log n). Without balance, a BST with sorted inserts degrades to O(n). AVL tree maintains |balance factor| ≤ 1 at every node. After insert/delete, if the factor becomes ±2, it performs rotations (LL, RR, LR, RL) to restore balance. Each rotation is O(1), so insert/delete remain O(log n).

Hints to guide your next attempt

  • Unbalanced BST worst case degrades to a linked list
  • Balance factor = left height - right height

Source tags: bst / avl / balanced-tree / rotations

QUESTION 9 / medium / conceptual

What is the time complexity of quicksort? When does it perform worst, and how do you avoid it?

Sample answer

Average: O(n log n). Worst: O(n²) — happens when the pivot always splits the array into sizes (0, n-1), e.g. sorted input with a fixed first-element pivot. Fixes: (1) Random pivot selection — makes worst case extremely unlikely. (2) Median-of-three pivot. (3) Use introsort (standard library) which switches to heapsort when recursion depth gets too large, guaranteeing O(n log n) worst case.

Hints to guide your next attempt

  • Worst case happens when pivots are always the smallest or largest element
  • Random pivot selection helps

Source tags: quicksort / sorting / pivot / time-complexity

QUESTION 10 / hard / coding

Given a binary tree, find the lowest common ancestor (LCA) of two nodes.

Sample answer

Recursive approach: - If root is null, p, or q → return root - Recurse left and right - If both return non-null → root is the LCA (one node is in each subtree) - Otherwise return whichever side returned non-null function lca(root, p, q): if not root or root == p or root == q: return root left = lca(root.left, p, q) right = lca(root.right, p, q) if left and right: return root return left or right Time: O(n), Space: O(h) recursion stack.

Hints to guide your next attempt

  • Recurse: if current node is p or q, return it
  • If both left and right return non-null, current node is the LCA

Source tags: binary-tree / lca / recursion

QUESTION 11 / easy / coding

Find the maximum subarray sum (Kadane's algorithm).

Sample answer

Kadane's algorithm — O(n) time, O(1) space: maxSum = currSum = nums[0] for num in nums[1:]: currSum = max(num, currSum + num) // extend or restart maxSum = max(maxSum, currSum) return maxSum Key insight: if currSum goes negative, the current subarray is a liability — start fresh from the next element. Works because a negative prefix can never help a future sum.

Hints to guide your next attempt

  • Track current sum and global max
  • Reset current sum to 0 when it goes negative

Source tags: array / dynamic-programming / kadane

QUESTION 12 / easy / coding

Rotate an array by K positions to the right in-place.

Sample answer

Three-reversal trick — O(n) time, O(1) space: k = k % n reverse(arr, 0, n-1) // reverse whole array reverse(arr, 0, k-1) // reverse first k reverse(arr, k, n-1) // reverse remainder Example: [1,2,3,4,5], k=2 → [5,4,3,2,1] → [4,5,3,2,1] → [4,5,1,2,3] ✓

Hints to guide your next attempt

  • Three reversal trick: reverse all, reverse first k, reverse rest
  • K = K % n to handle K > n

Source tags: array / in-place / rotation

Check understanding, not recognition

  • Can you explain the answer without reading it?
  • Can you show when the approach fails or needs a different assumption?
  • Can you connect the concept to a real project, task, or decision you have made?
  • Can you distinguish a simplified interview example from a production requirement?

A LITTLE MORE CLARITY

Frequently asked questions

How many Data Structures & Algorithms questions are shown here?

This public preview shows 12 questions with the supplied answers from the app's 60-question Data Structures & Algorithms bank. Full interactive practice requires signing in.

Should I memorize the sample answers?

Use them as reference explanations, not a script or a guarantee of technical correctness for every environment. Explain your assumptions, validate code against your language and version, and discuss trade-offs. Behavioral answers should be built from your own experience.

What do hints and solution steps mean?

Hints identify ideas to consider before reading the answer. When the source question includes steps, they are shown as a separate walkthrough. Example capacities and design targets in a solution are assumptions for that exercise, not real system statistics.

BUILD YOUR NEXT OPPORTUNITY

Practice Data Structures & Algorithms actively.

Sign in to use the full topic bank and interactive practice workspace.

Open Topic Practice