Data Structures and Algorithms Interview Questions (DSA MCQs)

Core data structures and algorithms questions asked by product and service companies, with a one-line explanation for each answer.

  1. Q1. Which data structure follows Last In, First Out (LIFO)?

    • A. Heap
    • B. Queue
    • C. Stack
    • D. Linked list
    Show answer

    Answer: C. Stack
    The last item pushed onto a stack is the first one popped.

  2. Q2. Which data structure follows First In, First Out (FIFO)?

    • A. Tree
    • B. Graph
    • C. Stack
    • D. Queue
    Show answer

    Answer: D. Queue
    A queue serves items in the order they arrive.

  3. Q3. What is the time complexity of binary search on a sorted array?

    • A. O(log n)
    • B. O(n log n)
    • C. O(1)
    • D. O(n)
    Show answer

    Answer: A. O(log n)
    Each step halves the search range.

  4. Q4. What is the worst-case time complexity of quicksort?

    • A. O(log n)
    • B. O(n)
    • C. O(n²)
    • D. O(n log n)
    Show answer

    Answer: C. O(n²)
    With bad pivots (e.g. already sorted input and first-element pivot) it degrades to O(n²). Average is O(n log n).

  5. Q5. Which sorting algorithm is guaranteed O(n log n) in the worst case?

    • A. Insertion sort
    • B. Quick sort
    • C. Merge sort
    • D. Bubble sort
    Show answer

    Answer: C. Merge sort
    Merge sort always splits in half and merges in linear time.

  6. Q6. Which traversal of a binary search tree gives the values in sorted order?

    • A. In-order
    • B. Post-order
    • C. Pre-order
    • D. Level-order
    Show answer

    Answer: A. In-order
    In-order visits left subtree, node, right subtree.

  7. Q7. What is the average time to look up a key in a hash table?

    • A. O(1)
    • B. O(n²)
    • C. O(log n)
    • D. O(n)
    Show answer

    Answer: A. O(1)
    Hashing jumps straight to the bucket; collisions make the worst case O(n).

  8. Q8. Which algorithm finds the shortest path in a graph with non-negative edge weights?

    • A. Dijkstra's algorithm
    • B. Kruskal's algorithm
    • C. Bubble sort
    • D. Depth-first search
    Show answer

    Answer: A. Dijkstra's algorithm
    Dijkstra greedily expands the closest unvisited vertex. Kruskal finds a minimum spanning tree, not shortest paths.

  9. Q9. Breadth-first search (BFS) typically uses which data structure?

    • A. Stack
    • B. Heap
    • C. Hash set only
    • D. Queue
    Show answer

    Answer: D. Queue
    BFS explores level by level, processing vertices in the order they were discovered.

  10. Q10. Depth-first search (DFS) can be implemented with recursion or which data structure?

    • A. Array of size 1
    • B. Queue
    • C. Stack
    • D. Priority queue
    Show answer

    Answer: C. Stack
    Recursion uses the call stack; an explicit stack works the same way.

  11. Q11. What is the maximum number of nodes in a binary tree of height h (root at height 0)?

    • A. 2^h
    • B. 2^(h+1) − 1
    • C. h²
    • D. 2h + 1
    Show answer

    Answer: B. 2^(h+1) − 1
    Each level doubles: 1 + 2 + 4 + … + 2^h = 2^(h+1) − 1.

  12. Q12. Inserting at the beginning of a singly linked list takes:

    • A. O(n)
    • B. O(log n)
    • C. O(1)
    • D. O(n²)
    Show answer

    Answer: C. O(1)
    Just point the new node at the old head.

  13. Q13. Accessing the i-th element of an array takes:

    • A. O(i²)
    • B. O(log n)
    • C. O(n)
    • D. O(1)
    Show answer

    Answer: D. O(1)
    Arrays use contiguous memory, so the address is computed directly.

  14. Q14. Which data structure is best for implementing a priority queue?

    • A. Linked list
    • B. Stack
    • C. Heap
    • D. Array sorted after every insert
    Show answer

    Answer: C. Heap
    A binary heap gives O(log n) insert and extract-min/max.

  15. Q15. What does dynamic programming rely on?

    • A. Random choices
    • B. Greedy choices only
    • C. Sorting the input first
    • D. Overlapping subproblems and optimal substructure
    Show answer

    Answer: D. Overlapping subproblems and optimal substructure
    DP stores answers to repeated subproblems instead of recomputing them.

  16. Q16. Which technique is used in the 'two-sum on a sorted array' O(n) solution?

    • A. Bit manipulation
    • B. Backtracking
    • C. Divide and conquer
    • D. Two pointers
    Show answer

    Answer: D. Two pointers
    Move the left pointer up when the sum is too small and the right pointer down when too big.

  17. Q17. What is the space complexity of merge sort on an array?

    • A. O(n)
    • B. O(log n)
    • C. O(1)
    • D. O(n²)
    Show answer

    Answer: A. O(n)
    Merging needs a temporary array of size n.

  18. Q18. A graph with V vertices stored as an adjacency matrix needs how much space?

    • A. O(V²)
    • B. O(V + E)
    • C. O(V)
    • D. O(E)
    Show answer

    Answer: A. O(V²)
    A V × V matrix. Adjacency lists need O(V + E).

  19. Q19. Which problem is typically solved with backtracking?

    • A. Computing an average
    • B. Binary search
    • C. N-Queens
    • D. Finding the maximum of an array
    Show answer

    Answer: C. N-Queens
    Backtracking tries choices and undoes them when they lead to a dead end.

  20. Q20. Detecting a cycle in a linked list in O(1) extra space uses:

    • A. A hash set
    • B. Sorting
    • C. Floyd's slow and fast pointers
    • D. Recursion
    Show answer

    Answer: C. Floyd's slow and fast pointers
    If there's a cycle, the fast pointer eventually meets the slow one. A hash set works but uses O(n) space.

  21. Q21. What is the time complexity of building a heap from n elements (heapify)?

    • A. O(n log n)
    • B. O(log n)
    • C. O(n²)
    • D. O(n)
    Show answer

    Answer: D. O(n)
    Bottom-up heapify is O(n) because most nodes are near the leaves.

  22. Q22. Which of these is a stable sorting algorithm?

    • A. Quick sort (typical)
    • B. Merge sort
    • C. Selection sort
    • D. Heap sort
    Show answer

    Answer: B. Merge sort
    Stable sorts keep equal elements in their original order.

  23. Q23. A balanced binary search tree (like AVL) guarantees search in:

    • A. O(n)
    • B. O(log n)
    • C. O(n log n)
    • D. O(1)
    Show answer

    Answer: B. O(log n)
    Balancing keeps the height about log n.

  24. Q24. Which data structure is used to check balanced parentheses?

    • A. Graph
    • B. Stack
    • C. Queue
    • D. Heap
    Show answer

    Answer: B. Stack
    Push openers, pop and compare on closers.

  25. Q25. Kadane's algorithm solves which problem?

    • A. Shortest path
    • B. Sorting
    • C. Maximum subarray sum
    • D. Minimum spanning tree
    Show answer

    Answer: C. Maximum subarray sum
    It tracks the best sum ending at each position in O(n).

  26. Q26. What is a 'trie' mainly used for?

    • A. Shortest paths
    • B. Prefix searches on strings
    • C. Sorting numbers
    • D. Matrix multiplication
    Show answer

    Answer: B. Prefix searches on strings
    Tries store strings character by character, making prefix lookups fast (autocomplete).

  27. Q27. Topological sort applies to which kind of graph?

    • A. Graphs with cycles
    • B. Complete graphs only
    • C. Any undirected graph
    • D. Directed acyclic graph (DAG)
    Show answer

    Answer: D. Directed acyclic graph (DAG)
    It orders tasks so every dependency comes before the task that needs it.

  28. Q28. Which algorithm builds a minimum spanning tree?

    • A. Dijkstra's algorithm
    • B. Kruskal's algorithm
    • C. KMP
    • D. Binary search
    Show answer

    Answer: B. Kruskal's algorithm
    Kruskal (and Prim) find the cheapest set of edges connecting all vertices.

  29. Q29. The 'sliding window' technique is most useful for:

    • A. Graph colouring
    • B. Tree traversal
    • C. Problems on contiguous subarrays or substrings
    • D. Sorting linked lists
    Show answer

    Answer: C. Problems on contiguous subarrays or substrings
    Expand and shrink a window instead of re-checking every subarray.

  30. Q30. What is the time complexity of inserting into a Python list at index 0?

    • A. O(log n)
    • B. O(n)
    • C. O(1)
    • D. O(n²)
    Show answer

    Answer: B. O(n)
    Every existing element shifts right. Use collections.deque for O(1) appends on both ends.

  31. Q31. Memoization means:

    • A. Sorting results
    • B. Writing code comments
    • C. Caching function results for inputs already computed
    • D. Using less memory
    Show answer

    Answer: C. Caching function results for inputs already computed
    It turns exponential recursion like naive Fibonacci into linear time.

  32. Q32. In a min-heap, the smallest element is always:

    • A. Anywhere
    • B. At the last leaf
    • C. At the root
    • D. In the middle
    Show answer

    Answer: C. At the root
    Every parent is ≤ its children, so the minimum sits at the top.

  33. Q33. How many edges does a tree with n nodes have?

    • A. 2n
    • B. n − 1
    • C. n + 1
    • D. n
    Show answer

    Answer: B. n − 1
    A tree is connected with no cycles, which needs exactly n − 1 edges.

  34. Q34. Which search works on unsorted data?

    • A. Linear search
    • B. Exponential search
    • C. Binary search
    • D. Interpolation search
    Show answer

    Answer: A. Linear search
    Without order, you have to check elements one by one.

  35. Q35. A recursive function without a base case will cause:

    • A. Faster execution
    • B. A syntax error
    • C. Stack overflow (RecursionError in Python)
    • D. It to return None
    Show answer

    Answer: C. Stack overflow (RecursionError in Python)
    Each call adds a stack frame until memory runs out.

Practised these? Now prove it.

Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.

Start a mock test