AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Data Structures & Algorithms aptitude questions

Arrays, linked lists, stacks, trees, sorting and complexity, asked as MCQs.

0 of 40 solved in this view

Formula sheetData Structures & Algorithms: the essentialsFormula sheetData Structures & Algorithms: the essentialsShow

Most questions ask for a complexity, a count, or the state after one operation.

Structures

  • Stack (last in, first out): recursion, bracket matching, postfix evaluation, depth-first search. Queue (first in, first out): breadth-first search and level-order traversal.
  • Linked list with only a head pointer: inserting at the head is O(1), at the tail O(n). Floyd's slow and fast pointers find a loop in O(1) space.
  • Binary trees: height h, counted in edges, allows at most 2^(h + 1) − 1 nodes; if every node has 0 or 2 children, leaves = internal nodes + 1. Inorder is left, root, right.
  • BST: inorder is sorted; search is O(log n) when balanced, O(n) in the worst case.
  • Heap in a 0-indexed array: the children of i are 2i + 1 and 2i + 2; insert and extract are O(log n).
  • Graphs: a complete graph has n(n − 1)/2 edges; adjacency lists take O(V + E) space, a matrix O(V²); a directed graph has a topological order exactly when it has no cycle.

Algorithms

  • Bubble, selection and insertion sort are O(n²); insertion sort is O(n) on sorted input; bubble sort's swaps equal the inversions.
  • Merge sort: O(n log n), stable, O(n) extra space. Quicksort: O(n log n) on average, O(n²) on sorted input with an end pivot. Heapsort: O(n log n), in place, unstable.
  • Binary search makes at most ⌊log₂ n⌋ + 1 comparisons. The Tower of Hanoi needs 2ⁿ − 1 moves.

Questions40

1

Evaluating a postfix expression

Evaluate the postfix expression 5 6 2 + * 12 4 / - using the usual stack method.

stack · expression

90s

Medium
2

Infix to postfix

Convert A + B * C - D / E to postfix, with * and / binding tighter than + and - and all operators left associative.

expression · conversion

90s

Medium
3

Evaluating a prefix expression

Evaluate the prefix expression + 9 * 3 - 5 2.

stack · expression

90s

Medium
4

An impossible stack output

The numbers 1, 2, 3, 4 are pushed onto a stack in that order, and a pop may be performed at any point. Which output sequence is impossible?

stack · permutation

120s

Hard
5

A queue from two stacks

A queue is built from two stacks: enqueue pushes onto stack A, and dequeue pops from stack B, refilling B by emptying A into it whenever B is empty.

stack · queue · amortised

90s

Medium
6

Elements in a circular queue

A circular queue occupies an array of 8 slots, with one slot always left empty so that full and empty can be told apart.

queue · circular

90s

Medium
7

Structure behind level-order traversal

Which data structure does a level-order (breadth-first) traversal of a binary tree rely on?

tree · traversal · queue

45s

Easy
8

Constant-time list operation

A singly linked list is kept with only a head pointer. Which operation runs in O(1) time?

linked list · complexity

60s

Easy
9

Pointer updates in a doubly linked list

A new node is inserted between two existing nodes of a doubly linked list. How many pointer fields have to be assigned?

linked list · doubly

60s

Medium
10

Circular list with a tail pointer

A circular singly linked list is kept with a single pointer to its last node. Which insertions are possible in O(1) time?

linked list · circular

75s

Medium
11

Detecting a loop in constant space

Which technique detects a loop in a singly linked list using only O(1) extra space?

linked list · cycle

60s

Easy
12

Maximum nodes for a given height

A binary tree has height 5, taking the height of a single-node tree to be 0. What is the maximum number of nodes it can contain?

tree · counting

60s

Easy
13

Leaves of a full binary tree

In a binary tree every node has either 0 or 2 children. If the tree contains 10 internal nodes, how many leaves does it have?

tree · counting

75s

Medium
14

Rebuilding a tree from two traversals

A binary tree has preorder A B D E C F and inorder D B E A C F. What is its postorder traversal?

tree · traversal

150s

Hard
15

The sorted traversal

Which traversal of a binary search tree visits the keys in ascending sorted order?

bst · traversal

45s

Easy
16

Worst-case BST search

What is the worst-case time complexity of searching for a key in a binary search tree holding n keys?

bst · complexity

45s

Easy
17

Deleting the root of a BST

A BST has 50 at the root, 30 and 70 as its children, 20 and 40 below 30, and 60 and 80 below 70. The root is deleted and replaced by its inorder successor.

bst · deletion

75s

Medium
18

From BST preorder to postorder

The preorder traversal of a binary search tree is 50, 30, 20, 40, 70, 60, 80. What is its postorder traversal?

bst · traversal

150s

Hard
19

Sparsest AVL tree

What is the minimum number of nodes in an AVL tree of height 4, counting the height of a single node as 0?

avl · tree · recurrence

150s

Hard
20

Inserting into a min-heap

A min-heap is stored as the array [10, 20, 15, 30, 40]. The key 5 is inserted. What does the array look like afterwards?

heap · insertion

105s

Medium
21

Spotting a valid max-heap

Reading each array as a complete binary tree in which the children of index i sit at 2i + 1 and 2i + 2, which array satisfies the max-heap property?

heap · array

105s

Medium
22

Extract-max on a heap

A max-heap is stored as [50, 30, 40, 10, 20, 35]. One extract-max operation is performed. What is the resulting array?

heap · deletion

120s

Hard
23

Quicksort on sorted input

Quicksort always takes the first element as its pivot. What is its running time on an array that is already sorted in ascending order?

sorting · quicksort · complexity

60s

Easy
24

One partition step

The array [7, 2, 1, 6, 8, 5, 3, 4] is partitioned by the Lomuto scheme with the last element as pivot: a boundary index i starts at −1, every element less…

sorting · quicksort · partition

180s

Hard
25

Which sort is unstable

Which of these sorting algorithms is not stable in its usual implementation?

sorting · stability

60s

Easy
26

Swaps in a bubble sort

How many swaps does bubble sort perform while sorting [5, 4, 3, 2, 1] into ascending order?

sorting · inversions

90s

Medium
27

Best case of insertion sort

What is the best-case running time of insertion sort, and on which input does it occur?

sorting · complexity

60s

Easy
28

The sort that needs a buffer

Which of these comparison sorts needs O(n) auxiliary space in its usual array implementation?

sorting · space

60s

Easy
29

Comparisons in a binary search

What is the maximum number of comparisons binary search makes on a sorted array of 1000 elements?

searching · complexity

75s

Medium
30

Finding both the minimum and the maximum

What is the minimum number of comparisons needed to find both the smallest and the largest of 100 distinct integers?

comparisons · lower bound

150s

Hard
31

Where linear probing lands

Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and linear probing. The keys 12, 22 and 32 are inserted in that order into an empty table.

hashing · probing

75s

Medium
32

Primary clustering

Which collision-resolution scheme suffers from primary clustering, in which occupied slots pile up into long consecutive runs?

hashing · clustering

75s

Medium
33

Edges of a complete graph

How many edges does a complete undirected graph on 12 vertices have?

graph · counting

45s

Easy
34

Space of an adjacency list

A graph has V vertices and E edges. How much space does its adjacency list representation take?

graph · representation

60s

Easy
35

Depth-first visiting order

An undirected graph has these adjacency lists: A depth-first search starts at A and always moves to the unvisited neighbour that comes first alphabetically.

graph · dfs

120s

Medium
36

Shortest path by breadth-first search

An undirected graph has these adjacency lists: A breadth-first search from A is used to find the shortest path to F. How many edges does that path contain?

graph · bfs · shortest path

105s

Medium
37

When a topological order exists

A directed graph has a topological ordering exactly when the graph...

graph · topological sort

60s

Easy
38

Address in a row-major array

An array A[10][20] of 4-byte integers is stored in row-major order with base address 1000, indices starting at 0. What is the address of A[3][5]?

array · addressing

90s

Medium
39

Counting recursive calls

The naive recursion fib(n) = fib(n − 1) + fib(n − 2), with fib(0) and fib(1) returned directly, is used to compute fib(5).

recursion · counting

105s

Medium
40

Tower of Hanoi

What is the minimum number of moves needed to solve the Tower of Hanoi puzzle with 6 disks?

recursion · hanoi

60s

Easy

Showing 40 of 40

Data Structures & Algorithms Aptitude Questions with Solutions

40 Data Structures & Algorithms questions with hints, worked solutions and time targets — 14 easy · 19 medium · 7 hard.

  • Section: Programming MCQs
  • Questions: 40
  • By difficulty: 14 easy · 19 medium · 7 hard
  • Cost: Free, unlimited on every plan

Arrays, linked lists, stacks, trees, sorting and complexity, asked as MCQs.

Data Structures & Algorithms: the essentials

Most questions ask for a complexity, a count, or the state after one operation.

Structures

  • Stack (last in, first out): recursion, bracket matching, postfix evaluation, depth-first search. Queue (first in, first out): breadth-first search and level-order traversal.
  • Linked list with only a head pointer: inserting at the head is O(1), at the tail O(n). Floyd's slow and fast pointers find a loop in O(1) space.
  • Binary trees: height h, counted in edges, allows at most 2^(h + 1) − 1 nodes; if every node has 0 or 2 children, leaves = internal nodes + 1. Inorder is left, root, right.
  • BST: inorder is sorted; search is O(log n) when balanced, O(n) in the worst case.
  • Heap in a 0-indexed array: the children of i are 2i + 1 and 2i + 2; insert and extract are O(log n).
  • Graphs: a complete graph has n(n − 1)/2 edges; adjacency lists take O(V + E) space, a matrix O(V²); a directed graph has a topological order exactly when it has no cycle.

Algorithms

  • Bubble, selection and insertion sort are O(n²); insertion sort is O(n) on sorted input; bubble sort's swaps equal the inversions.
  • Merge sort: O(n log n), stable, O(n) extra space. Quicksort: O(n log n) on average, O(n²) on sorted input with an end pivot. Heapsort: O(n log n), in place, unstable.
  • Binary search makes at most ⌊log₂ n⌋ + 1 comparisons. The Tower of Hanoi needs 2ⁿ − 1 moves.

Questions

  1. Evaluating a postfix expression Medium
    Evaluate the postfix expression 5 6 2 + * 12 4 / - using the usual stack method.
  2. Infix to postfix Medium
    Convert A + B * C - D / E to postfix, with * and / binding tighter than + and - and all operators left associative.
  3. Evaluating a prefix expression Medium
    Evaluate the prefix expression + 9 * 3 - 5 2.
  4. An impossible stack output Hard
    The numbers 1, 2, 3, 4 are pushed onto a stack in that order, and a pop may be performed at any point. Which output sequence is impossible?
  5. A queue from two stacks Medium
    A queue is built from two stacks: enqueue pushes onto stack A, and dequeue pops from stack B, refilling B by emptying A into it whenever B is empty.
  6. Elements in a circular queue Medium
    A circular queue occupies an array of 8 slots, with one slot always left empty so that full and empty can be told apart.
  7. Structure behind level-order traversal Easy
    Which data structure does a level-order (breadth-first) traversal of a binary tree rely on?
  8. Constant-time list operation Easy
    A singly linked list is kept with only a head pointer. Which operation runs in O(1) time?
  9. Pointer updates in a doubly linked list Medium
    A new node is inserted between two existing nodes of a doubly linked list. How many pointer fields have to be assigned?
  10. Circular list with a tail pointer Medium
    A circular singly linked list is kept with a single pointer to its last node. Which insertions are possible in O(1) time?
  11. Detecting a loop in constant space Easy
    Which technique detects a loop in a singly linked list using only O(1) extra space?
  12. Maximum nodes for a given height Easy
    A binary tree has height 5, taking the height of a single-node tree to be 0. What is the maximum number of nodes it can contain?
  13. Leaves of a full binary tree Medium
    In a binary tree every node has either 0 or 2 children. If the tree contains 10 internal nodes, how many leaves does it have?
  14. Rebuilding a tree from two traversals Hard
    A binary tree has preorder A B D E C F and inorder D B E A C F. What is its postorder traversal?
  15. The sorted traversal Easy
    Which traversal of a binary search tree visits the keys in ascending sorted order?
  16. Worst-case BST search Easy
    What is the worst-case time complexity of searching for a key in a binary search tree holding n keys?
  17. Deleting the root of a BST Medium
    A BST has 50 at the root, 30 and 70 as its children, 20 and 40 below 30, and 60 and 80 below 70. The root is deleted and replaced by its inorder successor.
  18. From BST preorder to postorder Hard
    The preorder traversal of a binary search tree is 50, 30, 20, 40, 70, 60, 80. What is its postorder traversal?
  19. Sparsest AVL tree Hard
    What is the minimum number of nodes in an AVL tree of height 4, counting the height of a single node as 0?
  20. Inserting into a min-heap Medium
    A min-heap is stored as the array [10, 20, 15, 30, 40]. The key 5 is inserted. What does the array look like afterwards?
  21. Spotting a valid max-heap Medium
    Reading each array as a complete binary tree in which the children of index i sit at 2i + 1 and 2i + 2, which array satisfies the max-heap property?
  22. Extract-max on a heap Hard
    A max-heap is stored as [50, 30, 40, 10, 20, 35]. One extract-max operation is performed. What is the resulting array?
  23. Quicksort on sorted input Easy
    Quicksort always takes the first element as its pivot. What is its running time on an array that is already sorted in ascending order?
  24. One partition step Hard
    The array [7, 2, 1, 6, 8, 5, 3, 4] is partitioned by the Lomuto scheme with the last element as pivot: a boundary index i starts at −1, every element less…
  25. Which sort is unstable Easy
    Which of these sorting algorithms is not stable in its usual implementation?
  26. Swaps in a bubble sort Medium
    How many swaps does bubble sort perform while sorting [5, 4, 3, 2, 1] into ascending order?
  27. Best case of insertion sort Easy
    What is the best-case running time of insertion sort, and on which input does it occur?
  28. The sort that needs a buffer Easy
    Which of these comparison sorts needs O(n) auxiliary space in its usual array implementation?
  29. Comparisons in a binary search Medium
    What is the maximum number of comparisons binary search makes on a sorted array of 1000 elements?
  30. Finding both the minimum and the maximum Hard
    What is the minimum number of comparisons needed to find both the smallest and the largest of 100 distinct integers?
  31. Where linear probing lands Medium
    Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and linear probing. The keys 12, 22 and 32 are inserted in that order into an empty table.
  32. Primary clustering Medium
    Which collision-resolution scheme suffers from primary clustering, in which occupied slots pile up into long consecutive runs?
  33. Edges of a complete graph Easy
    How many edges does a complete undirected graph on 12 vertices have?
  34. Space of an adjacency list Easy
    A graph has V vertices and E edges. How much space does its adjacency list representation take?
  35. Depth-first visiting order Medium
    An undirected graph has these adjacency lists: A depth-first search starts at A and always moves to the unvisited neighbour that comes first alphabetically.
  36. Shortest path by breadth-first search Medium
    An undirected graph has these adjacency lists: A breadth-first search from A is used to find the shortest path to F. How many edges does that path contain?
  37. When a topological order exists Easy
    A directed graph has a topological ordering exactly when the graph...
  38. Address in a row-major array Medium
    An array A[10][20] of 4-byte integers is stored in row-major order with base address 1000, indices starting at 0. What is the address of A[3][5]?
  39. Counting recursive calls Medium
    The naive recursion fib(n) = fib(n − 1) + fib(n − 2), with fib(0) and fib(1) returned directly, is used to compute fib(5).
  40. Tower of Hanoi Easy
    What is the minimum number of moves needed to solve the Tower of Hanoi puzzle with 6 disks?

Placement tests that ask data structures & algorithms