Data Structures & Algorithms quiz

Arrays, lists, trees, graphs, sorting, searching and dynamic programming.

69 questionsInstant feedbackFree

Set up your quiz

Difficulty
Number of questions

Tip: press 1–4 to answer and Enter for the next question.

All 69 questions with answers
  1. What is a data structure?

    • A type of database only
    • A way of organizing and storing data for efficient use (correct)
    • A programming language
    • A sorting algorithm
  2. What does an algorithm's time complexity measure?

    • The year it was invented
    • The memory of the computer
    • The number of lines of code
    • How its running time grows as the input size grows (correct)
  3. Which data structure follows FIFO (first in, first out)?

    • Queue (correct)
    • Stack
    • Tree
    • Graph
  4. Which operation adds an element to the top of a stack?

    • Push (correct)
    • Enqueue
    • Peek
    • Pop
  5. Which operation removes the top element of a stack?

    • Pop (correct)
    • Dequeue
    • Push
    • Insert
  6. What is the time complexity of accessing an array element by index?

    • O(n)
    • O(log n)
    • O(1) (correct)
    • O(n^2)
  7. In a singly linked list, each node stores data and what?

    • A pointer to the previous node only
    • The list length
    • A pointer to the next node (correct)
    • An index
  8. What is the first node of a linked list called?

    • Tail
    • Leaf
    • Root
    • Head (correct)
  9. What is the top node of a tree called?

    • Parent of all leaves only
    • Head
    • Leaf
    • Root (correct)
  10. A node with no children in a tree is called a:

    • Edge
    • Leaf (correct)
    • Branch
    • Root
  11. How many children can each node have in a binary tree at most?

    • 1
    • Unlimited
    • 2 (correct)
    • 3
  12. What does Big O notation describe?

    • The memory address of data
    • How an algorithm's time or space grows with input size (correct)
    • The size of the code
    • The exact running time in seconds
  13. What is the time complexity of linear search?

    • O(1)
    • O(n) (correct)
    • O(log n)
    • O(n log n)
  14. What does binary search require?

    • A linked list
    • A hash table
    • A sorted collection (correct)
    • Unique values only
  15. What is the time complexity of binary search?

    • O(1)
    • O(log n) (correct)
    • O(n)
    • O(n^2)
  16. What is recursion?

    • A type of array
    • A loop that never ends
    • A sorting method
    • A function that calls itself (correct)
  17. What is a base case in recursion?

    • The first function call
    • The largest input
    • The condition that stops further recursive calls (correct)
    • The return type
  18. Which data structure is used for function calls in most languages?

    • Heap
    • Stack (correct)
    • Graph
    • Queue
  19. Which data structure is used for breadth-first search?

    • Queue (correct)
    • Hash table
    • Stack
    • Heap
  20. What does a graph consist of?

    • Vertices and edges (correct)
    • Keys and values only
    • Roots and leaves only
    • Rows and columns
  21. What is the worst-case time complexity of bubble sort?

    • O(n)
    • O(n log n)
    • O(log n)
    • O(n^2) (correct)
  22. What is the average time complexity of merge sort?

    • O(n)
    • O(log n)
    • O(n log n) (correct)
    • O(n^2)
  23. What is the worst-case time complexity of quicksort?

    • O(n)
    • O(log n)
    • O(n^2) (correct)
    • O(n log n)
  24. Which sorting algorithm is stable and always O(n log n)?

    • Heap sort
    • Quicksort
    • Merge sort (correct)
    • Selection sort
  25. What is the extra space used by merge sort on arrays?

    • O(n) (correct)
    • O(n^2)
    • O(1)
    • O(log n)
  26. What does a hash function do in a hash table?

    • Counts the elements
    • Sorts the keys
    • Encrypts the values
    • Maps a key to an index (correct)
  27. What is a collision in a hash table?

    • A full table
    • A key that is too long
    • A deleted key
    • Two keys mapping to the same index (correct)
  28. What is the average time to look up a key in a hash table?

    • O(log n)
    • O(n log n)
    • O(1) (correct)
    • O(n)
  29. In a binary search tree, where are values smaller than a node stored?

    • In its right subtree
    • In the parent
    • In its left subtree (correct)
    • In a separate list
  30. Which traversal of a binary search tree visits values in sorted order?

    • Post-order
    • Level-order
    • In-order (correct)
    • Pre-order
  31. In pre-order traversal, when is the current node visited?

    • After both subtrees
    • Only if it is a leaf
    • Between the subtrees
    • Before its left and right subtrees (correct)
  32. What is the height of a balanced binary tree with n nodes?

    • About n^2
    • About n
    • About n/2
    • About log n (correct)
  33. What is the worst-case search time in an unbalanced binary search tree?

    • O(n) (correct)
    • O(n log n)
    • O(log n)
    • O(1)
  34. What is a heap?

    • A hash table with buckets
    • A complete binary tree where each parent is ordered relative to its children (correct)
    • A linked list of blocks
    • A sorted array
  35. What does a min-heap keep at its root?

    • The newest element
    • The smallest element (correct)
    • The largest element
    • The median
  36. What is the time to insert into a binary heap?

    • O(n log n)
    • O(log n) (correct)
    • O(n)
    • O(1)
  37. Which data structure is usually used to implement a priority queue?

    • Stack
    • Array without ordering
    • Heap (correct)
    • Linked list only
  38. Which algorithm explores as deep as possible before backtracking?

    • Depth-first search (correct)
    • Breadth-first search
    • Binary search
    • Dijkstra's algorithm
  39. Which graph representation uses a V × V matrix?

    • Adjacency list
    • Edge list
    • Incidence tree
    • Adjacency matrix (correct)
  40. Which algorithm finds shortest paths from one source with non-negative edge weights?

    • Prim's algorithm
    • Dijkstra's algorithm (correct)
    • Depth-first search
    • Kruskal's algorithm
  41. What is dynamic programming?

    • Random guessing
    • Solving problems by combining solutions to overlapping subproblems and storing them (correct)
    • Using dynamic memory only
    • Programming that changes at runtime
  42. What is memoization?

    • Caching results of function calls to avoid recomputation (correct)
    • Allocating memory early
    • Writing notes in code
    • Memorizing algorithms
  43. What is the time complexity of inserting at the head of a singly linked list?

    • O(n log n)
    • O(log n)
    • O(1) (correct)
    • O(n)
  44. What is a doubly linked list?

    • A list where each node links to both the next and previous nodes (correct)
    • A list with two heads
    • A list stored twice
    • Two linked lists joined together
  45. Which technique uses two indexes moving through an array to solve problems in linear time?

    • Divide and conquer
    • Two pointers (correct)
    • Hashing
    • Backtracking
  46. What is a greedy algorithm?

    • One that makes the locally best choice at each step (correct)
    • One that tries every possibility
    • One that uses recursion only
    • One that uses the most memory
  47. What is the time complexity of naive recursive Fibonacci?

    • O(n^2)
    • O(n)
    • O(log n)
    • O(2^n) (correct)
  48. What is the time complexity of Fibonacci computed with dynamic programming?

    • O(2^n)
    • O(n^2)
    • O(1) always
    • O(n) (correct)
  49. What does the sliding window technique help with?

    • Matrix multiplication
    • Problems on contiguous subarrays or substrings (correct)
    • Graph colouring
    • Sorting arrays
  50. Which algorithm finds a minimum spanning tree by adding the cheapest edge that does not form a cycle?

    • Kruskal's algorithm (correct)
    • Floyd-Warshall
    • Bellman-Ford
    • Dijkstra's algorithm
  51. Which algorithm can handle negative edge weights and detect negative cycles?

    • Dijkstra's algorithm
    • Breadth-first search
    • Bellman-Ford (correct)
    • Prim's algorithm
  52. Which algorithm computes shortest paths between all pairs of vertices in O(V^3)?

    • Dijkstra's algorithm
    • Topological sort
    • Floyd-Warshall (correct)
    • Kruskal's algorithm
  53. What is a topological sort?

    • Sorting a tree by height
    • Ordering vertices by degree
    • Sorting numbers by their digits
    • A linear ordering of a directed acyclic graph's vertices respecting edge directions (correct)
  54. Which data structure efficiently supports union and find for connected components?

    • Deque
    • Segment tree
    • Disjoint set union (correct)
    • Trie
  55. What is a trie mainly used for?

    • Balancing trees
    • Storing graphs
    • Fast prefix lookups on strings (correct)
    • Sorting numbers
  56. What is an AVL tree?

    • A heap variant
    • A tree with only leaves
    • A self-balancing binary search tree (correct)
    • A graph with no cycles
  57. What is the amortized time to append to a dynamic array?

    • O(n^2)
    • O(1) (correct)
    • O(n)
    • O(log n)
  58. What is the time complexity of building a heap from n elements with heapify?

    • O(n) (correct)
    • O(n^2)
    • O(log n)
    • O(n log n)
  59. Which problem is solved by the classic 0/1 knapsack dynamic programming approach?

    • Sorting items by weight
    • Packing items into the fewest boxes
    • Finding the shortest path
    • Choosing items with maximum value under a weight limit (correct)
  60. What is the time complexity of the dynamic programming solution for the longest common subsequence of strings of length m and n?

    • O(m × n) (correct)
    • O(2^(m+n))
    • O(m + n)
    • O(log m)
  61. How can you detect a cycle in a linked list using O(1) extra space?

    • Store each node in a hash set
    • Floyd's slow and fast pointers (correct)
    • Sort the list
    • Reverse the list twice
  62. What is backtracking?

    • Building candidates step by step and abandoning ones that cannot lead to a solution (correct)
    • Traversing a list backwards
    • Sorting in reverse
    • Undoing a git commit
  63. What is the space complexity of a recursive DFS on a graph in the worst case?

    • O(1)
    • O(log V)
    • O(V) (correct)
    • O(E^2)
  64. What is a segment tree used for?

    • Sorting segments of an array
    • Answering range queries like sums or minimums with updates in O(log n) (correct)
    • Storing strings by prefix
    • Finding cycles
  65. Which sorting algorithm is not comparison-based?

    • Quicksort
    • Merge sort
    • Heap sort
    • Counting sort (correct)
  66. What is the lower bound for comparison-based sorting in the worst case?

    • Ω(n log n) (correct)
    • Ω(n^2)
    • Ω(n)
    • Ω(log n)
  67. What is the time complexity of Dijkstra's algorithm with a binary heap?

    • O(E^2)
    • O(V + E)
    • O(V^3)
    • O((V + E) log V) (correct)
  68. What does a stable sort guarantee?

    • Equal elements keep their original relative order (correct)
    • It never uses extra memory
    • It never swaps elements
    • It always runs in O(n log n)
  69. Which problem asks for the minimum number of edits to turn one string into another?

    • Edit (Levenshtein) distance (correct)
    • Coin change
    • Longest increasing subsequence
    • Knapsack