Top 7 Coding Interview Algorithms
- 33Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Binary search requires the list to be:
Binary search requires the list to be:
33 questions
- 1
Binary search requires the list to be:
- A
Sorted in ascending or descending order
- B
Containing unique elements
- C
Stored in an array
- D
Unsorted
Correct answer
ASorted in ascending or descending order
Explanation
Binary search only works on sorted lists because it relies on comparing the target to the middle element to eliminate half of the list.
- A
- 2
The time complexity of binary search in the worst case is:
- A
O(n)
- B
O(log n)
- C
O(n^2)
- D
O(n log n)
Correct answer
BO(log n)
Explanation
Binary search divides the search space in half each time, resulting in O(log n) time complexity.
- A
- 3
In the guessing game example with numbers 1 to 10,000, how many guesses does binary search take in the worst case?
- A
10
- B
10,000
- C
100
- D
14
Correct answer
D14
Explanation
The worst-case number of guesses is the ceiling of log2ā(10000)ā13.3, so 14 guesses.
- A
- 4
Depth-first search (DFS) visits nodes by:
- A
Going as deep as possible along one branch before backtracking
- B
Going level by level
- C
Using a queue to process nodes
- D
Randomly selecting unvisited nodes
Correct answer
AGoing as deep as possible along one branch before backtracking
Explanation
DFS explores as far as possible along each branch before backtracking, which is the 'depth-first' approach.
- A
- 5
The time complexity of DFS is:
- A
O(V + E)
- B
O(E)
- C
O(V log E)
- D
O(V)
Correct answer
AO(V + E)
Explanation
DFS visits each vertex and edge once, leading to O(V+E) time complexity.
- A
- 6
In DFS, what is the purpose of the visited array?
- A
To calculate the depth of each node
- B
To keep track of backtracking points
- C
To store the order of traversal
- D
To avoid revisiting nodes that have already been visited
Correct answer
DTo avoid revisiting nodes that have already been visited
Explanation
The visited array tracks which nodes have been visited to prevent infinite loops and redundant exploration.
- A
- 7
A real-life application of DFS mentioned in the video is:
- A
Predicting stock prices
- B
Sorting a list
- C
Chess algorithms
- D
Solving a maze
Correct answer
DSolving a maze
Explanation
The video mentions that DFS was originally created for solving mazes.
- A
- 8
Breadth-first search (BFS) processes nodes in what order?
- A
Level by level (breadth-first)
- B
Depth-first order
- C
Reverse order
- D
Random order
Correct answer
ALevel by level (breadth-first)
Explanation
BFS visits all nodes at the current level before moving to the next level.
- A
- 9
Which data structure does BFS use to keep track of nodes to visit next?
- A
Queue
- B
Set
- C
Priority queue
- D
Stack
Correct answer
AQueue
Explanation
BFS uses a queue (neighbours queue) to process nodes level by level.
- A
- 10
The time complexity of BFS is:
- A
O(V)
- B
O(V log E)
- C
O(V + E)
- D
O(E)
Correct answer
CO(V + E)
Explanation
Like DFS, BFS also visits each vertex and edge once, giving O(V+E) time complexity.
- A
- 11
A real-life application of BFS mentioned in the video is:
- A
Searching a phone book
- B
Chess algorithms
- C
Sorting a list
- D
Solving a maze
Correct answer
BChess algorithms
Explanation
The video mentions that BFS is used in chess algorithms to evaluate moves level by level.
- A
- 12
Which algorithm is best suited for finding an element in a sorted list?
- A
Linear search
- B
Binary search
- C
Breadth-first search
- D
Depth-first search
Correct answer
BBinary search
Explanation
Binary search is specifically designed for sorted lists and is efficient O(log n) compared to linear search O(n).
- A
- 13
In DFS, after reaching a dead end, the algorithm:
- A
Stops
- B
Restarts from the root
- C
Backtracks to the most recent node with unvisited branches
- D
Switches to BFS
Correct answer
CBacktracks to the most recent node with unvisited branches
Explanation
DFS backtracks to the previous node and continues exploring unvisited branches.
- A
- 14
Insertion sort works by:
- A
Selecting a pivot and partitioning the list
- B
Swapping adjacent elements repeatedly
- C
Dividing the list into halves recursively
- D
Comparing each element with the previous ones and inserting it in the correct position
Correct answer
DComparing each element with the previous ones and inserting it in the correct position
Explanation
Insertion sort builds the sorted list one element at a time by inserting each new element into its proper place among the previously sorted elements.
- A
- 15
The best-case time complexity of insertion sort is:
- A
O(n log n)
- B
O(n)
- C
O(n^2)
- D
O(log n)
Correct answer
BO(n)
Explanation
When the list is already sorted, insertion sort only makes n-1 comparisons, so O(n).
- A
- 16
The worst-case time complexity of insertion sort is:
- A
O(n log n)
- B
O(n^2)
- C
O(n)
- D
O(log n)
Correct answer
BO(n^2)
Explanation
When the list is reverse sorted, insertion sort must compare and shift each element, leading to O(n^2) time.
- A
- 17
Insertion sort is most efficient when:
- A
The list is stored in a linked list
- B
The list contains many duplicate values
- C
The list is large and unsorted
- D
The list is small or nearly sorted
Correct answer
DThe list is small or nearly sorted
Explanation
Insertion sort has O(n) best case on nearly sorted data and is simple for small lists.
- A
- 18
Merge sort is an example of which algorithmic paradigm?
- A
Dynamic programming
- B
Divide and conquer
- C
Greedy
- D
Backtracking
Correct answer
BDivide and conquer
Explanation
Merge sort splits the list into halves recursively (divide) and then merges sorted halves (conquer).
- A
- 19
Merge sort is a recursive algorithm. True or false?
- A
True, it calls itself on subarrays
- B
True, but only in the worst case
- C
False, it uses iteration exclusively
- D
False, it is an iterative algorithm
Correct answer
ATrue, it calls itself on subarrays
Explanation
Merge sort recursively divides the array into halves until subarrays of size 1, then merges them back up.
- A
- 20
The time complexity of merge sort in both best and worst cases is:
- A
O(n)
- B
O(n^2)
- C
O(log n)
- D
O(n log n)
Correct answer
DO(n log n)
Explanation
Merge sort always divides the list into halves (log n levels) and merges each level (n work), resulting in O(n log n).
- A
- 21
The space complexity of merge sort is:
- A
O(log n)
- B
O(1)
- C
O(n log n)
- D
O(n)
Correct answer
DO(n)
Explanation
Merge sort requires O(n) auxiliary space for merging temporary arrays.
- A
- 22
Quick sort selects a pivot and then:
- A
Partitions the list into elements less than and greater than the pivot
- B
Inserts the pivot at the correct position
- C
Merges two sorted halves
- D
Swaps adjacent elements until sorted
Correct answer
APartitions the list into elements less than and greater than the pivot
Explanation
Quick sort partitions the list around a pivot so that elements less than pivot come before it and greater come after.
- A
- 23
The best-case time complexity of quick sort is:
- A
O(log n)
- B
O(n)
- C
O(n^2)
- D
O(n log n)
Correct answer
DO(n log n)
Explanation
When the pivot divides the list into two roughly equal halves, quick sort runs in O(n log n).
- A
- 24
The worst-case time complexity of quick sort is:
- A
O(n^2)
- B
O(n)
- C
O(n log n)
- D
O(log n)
Correct answer
AO(n^2)
Explanation
If the pivot is always the smallest or largest element, quick sort degrades to O(n^2).
- A
- 25
According to the video, which sorting algorithm is generally the fastest on average when implemented correctly?
- A
Quick sort
- B
Merge sort
- C
They are all equally fast
- D
Insertion sort
Correct answer
AQuick sort
Explanation
The video states that quick sort, when optimized, is 2-3 times faster than merge sort on average.
- A
- 26
The space complexity of quick sort (in-place) is:
- A
O(1)
- B
O(log n)
- C
O(n)
- D
O(n log n)
Correct answer
BO(log n)
Explanation
In-place quick sort uses O(log n) space for the recursion stack.
- A
- 27
One drawback of quick sort mentioned in the video is:
- A
It is harder to implement correctly and any inefficiency can cause it to run slower
- B
It cannot be used on large lists
- C
It requires a lot of extra memory
- D
It is slower than merge sort on average
Correct answer
AIt is harder to implement correctly and any inefficiency can cause it to run slower
Explanation
The video notes that quick sort is harder to implement correctly; any mistake can degrade performance.
- A
- 28
A greedy algorithm makes decisions based on:
- A
Random selection
- B
The worst possible choice
- C
The best local choice at each step
- D
The overall optimal solution
Correct answer
CThe best local choice at each step
Explanation
Greedy algorithms choose the best option at each decision point without considering future consequences.
- A
- 29
Why might a greedy algorithm not find the optimal solution?
- A
It only looks at the immediate best option, which may lead to a suboptimal path
- B
It is too slow
- C
It requires exponential time
- D
It considers all future possibilities
Correct answer
AIt only looks at the immediate best option, which may lead to a suboptimal path
Explanation
The local optimal choice may not lead to the global optimum, as shown in the example with path costs.
- A
- 30
When is it acceptable to use a greedy algorithm?
- A
When the input size is small
- B
When the optimal solution is easy to compute
- C
When the problem is too large to compute the exact optimal solution
- D
When you need the fastest algorithm possible
Correct answer
CWhen the problem is too large to compute the exact optimal solution
Explanation
Greedy algorithms are used when the exact optimal solution is computationally infeasible, to get a reasonably good answer.
- A
- 31
In the traveling salesman problem with n cities, the number of possible routes is:
- A
n!
- B
2^n
- C
n^2
- D
(n-1)!/2
Correct answer
D(n-1)!/2
Explanation
The video gives the formula (nā1)!/2 for total possible routes in the traveling salesman problem.
- A
- 32
In the path cost example from the video, the optimal cost was $3, but the greedy algorithm achieved a cost of:
- A
9
- B
16
- C
7
- D
3
Correct answer
B16
Explanation
The greedy algorithm chose 7 and then 9 totaling 16, while the optimal path cost only 3.
- A
- 33
Which of the following is a characteristic of greedy algorithms?
- A
They are the most efficient sorting algorithms
- B
They make locally optimal choices
- C
They always produce the optimal solution
- D
They require backtracking
Correct answer
BThey make locally optimal choices
Explanation
Greedy algorithms are defined by making the best local decision at each step.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.