Algorithms and Data Structures Junior interview questions and answers

75 questions for a Algorithms and Data Structures (Junior) interview - with correct answers, explanations and links to the docs.

Take this quiz
  1. 1.What does Big O notation describe?
  2. 2.What is the time complexity of this function?
  3. 3.What is the time complexity of two nested loops over the same array of length n?
  4. 4.Why is O(2n + 10) written simply as O(n)?
  5. 5.Which complexity grows the slowest as n increases?
  6. 6.What is the complexity of reading arr[i] from a JavaScript array?
  7. 7.Which array operation is usually O(n) in JavaScript?
  8. 8.What is the complexity of arr.includes(x) on an unsorted array?
  9. 9.Why is set.has(x) usually faster than arr.includes(x) for large collections?
  10. 10.What is the space complexity of this function?
  11. 11.What does O(1) extra space mean for an algorithm?
  12. 12.What is the main idea of a hash table?
  13. 13.What is a collision in a hash table?
  14. 14.What is the worst-case lookup time in a hash table?
  15. 15.When is a Map a better choice than a plain object for a dictionary?
  16. 16.What is the simplest way to remove duplicates from an array of primitives?
  17. 17.How do you check in O(n) whether an array contains duplicates?
  18. 18.What does the stack data structure guarantee?
  19. 19.Which task is a natural fit for a stack?
  20. 20.What does a queue guarantee?
  21. 21.Why can a queue built on push and shift become slow on large inputs?
  22. 22.Where does the JavaScript runtime itself use a stack?
  23. 23.What is a singly linked list?
  24. 24.What is the cost of getting the k-th element of a linked list?
  25. 25.Which operation is O(1) in a linked list but O(n) at the start of an array?
  26. 26.What does every correct recursive function need?
  27. 27.What happens when you call this function?
  28. 28.Why is this Fibonacci function slow for n = 40?
  29. 29.What is memoization?
  30. 30.What is linear search?
  31. 31.What does binary search require from the data?
  32. 32.What is the time complexity of binary search?
  33. 33.Roughly how many comparisons does binary search need in a sorted array of 1,000,000 elements?
  34. 34.What does this code return?
  35. 35.What should the comparator (a, b) => ... return for sort to put a before b?
  36. 36.What is important to remember about arr.sort()?
  37. 37.What does a stable sort guarantee?
  38. 38.What is the typical time complexity of a good general-purpose comparison sort?
  39. 39.How does bubble sort work?
  40. 40.When is insertion sort a good choice?
  41. 41.How do you reverse an array in place with O(1) extra memory?
  42. 42.What is the "two pointers" technique?
  43. 43.How do you check whether a string is a palindrome in O(n) time and O(1) extra space?
  44. 44.How do you check that two strings are anagrams in O(n)?
  45. 45.What is the complexity of building a string with += in a loop in modern JavaScript engines?
  46. 46.What is a tree in data structures?
  47. 47.What is a leaf in a tree?
  48. 48.What is the height of a tree?
  49. 49.What property does a binary search tree (BST) have?
  50. 50.What is the time to find a value in a balanced BST with n nodes?
  51. 51.What happens if you insert the values 1, 2, 3, 4, 5 in order into a simple (non-balancing) BST?
  52. 52.Which structure is the DOM of a web page?
  53. 53.What is the difference between depth-first (DFS) and breadth-first (BFS) traversal?
  54. 54.Which data structure does breadth-first search (BFS) use?
  55. 55.What is a graph?
  56. 56.What is the difference between a directed and an undirected graph?
  57. 57.How is a graph usually stored when it has many vertices but few edges?
  58. 58.What does this adjacency list describe?
  59. 59.What is the complexity of finding the maximum of an unsorted array?
  60. 60.Why can Math.max(...arr) fail on a very large array?
  61. 61.How do you count how many times each word appears in a list in O(n)?
  62. 62.Two Sum: find two numbers in an unsorted array that add up to target. What is the O(n) approach?
  63. 63.What does "amortized O(1)" mean for arr.push?
  64. 64.What is the time complexity of the isPrime check below?
  65. 65.What is the complexity of this loop?
  66. 66.What is the complexity of looking up a key in a plain JavaScript object, obj[key]?
  67. 67.What is a prefix sum array?
  68. 68.What is the complexity of arr.slice() (copying the whole array)?
  69. 69.What is a matrix (2D grid) usually represented as in JavaScript?
  70. 70.Why should you avoid new Array(3).fill([]) for a 3 × 3 grid?
  71. 71.What is a greedy algorithm?
  72. 72.What does "brute force" mean for an algorithm?
  73. 73.An algorithm is O(n²). Roughly how much longer does it take when the input grows from 1,000 to 10,000 items?
  74. 74.Why is a Set of objects not useful for removing duplicate objects with the same content?
  75. 75.How does arr.splice(i, 1) in the middle of an array affect performance?