Top 7 Coding Interview Algorithms
- 29Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What condition must a list satisfy for binary search to work correctly?
29 questions
- 1
What condition must a list satisfy for binary search to work correctly?
Model answer
The list must be sorted in order (ascending or descending).
Explanation
Binary search works by repeatedly dividing the search interval in half, which relies on the list being sorted to determine whether the target is in the left or right half.
- 2
Compare the worst-case time complexities of linear search and binary search.
Model answer
Linear search has worst-case runtime O(n), while binary search has worst-case runtime O(logn).
Explanation
In linear search, each element may need to be checked, so O(n). Binary search halves the search space each step, so O(logn).
- 3
What is the core idea behind Depth-First Search (DFS)?
Model answer
DFS starts at the root node and explores as far down a single branch as possible before backtracking to explore other branches.
Explanation
The algorithm goes deep into one path until it cannot go further, then uses backtracking to return to previous nodes with unvisited branches.
- 4
What data structure is commonly used in DFS to keep track of visited nodes?
Model answer
A visited array (or set) is used to record which nodes have already been visited.
Explanation
The visited array prevents revisiting nodes and ensures the algorithm terminates, as it checks this array before exploring a node.
- 5
In DFS, what is the term for returning to a previous node to explore unvisited branches?
Model answer
Backtracking.
Explanation
When DFS reaches a leaf node or a dead end, it backtracks to the most recent node that has an unexplored branch and continues from there.
- 6
Give a real-life example where Depth-First Search is used.
Model answer
Solving a maze: from the entrance, the algorithm follows each path to its end, and if it hits a wall, it backtracks to try another path.
Explanation
Maze solving naturally uses DFS because it explores one path fully before trying alternatives, similar to backtracking until the exit is found.
- 7
What is the time complexity of DFS in terms of vertices (V) and edges (E)?
Model answer
O(V+E).
Explanation
DFS visits each vertex once and examines each edge once, so the runtime is proportional to the total number of vertices and edges.
- 8
What is the core idea behind Breadth-First Search (BFS)?
Model answer
BFS starts at the root node and explores all nodes at the current depth level before moving to the next level.
Explanation
BFS uses a queue to process nodes level by level, ensuring that all nodes at distance k are visited before any node at distance k+1.
- 9
What two data structures are essential for implementing BFS?
Model answer
A visited array (or set) and a queue (often called the neighbors queue).
Explanation
The visited array prevents revisiting nodes, and the queue stores nodes to be explored in the order they are discovered, ensuring level-order traversal.
- 10
Give a real-life example where Breadth-First Search is applied.
Model answer
Chess algorithms: the algorithm evaluates all possible moves from the current position, then all moves from those positions, exploring level by level.
Explanation
In chess engines, BFS can evaluate future moves by considering all possibilities at each turn before going deeper, similar to analyzing all next moves from the current state.
- 11
How does BFS differ from DFS in the order nodes are visited?
Model answer
BFS visits nodes level by level (breadth-first), while DFS visits nodes by going deep down one branch first (depth-first).
Explanation
BFS uses a queue to process all nodes at the same depth before deeper nodes; DFS uses recursion or a stack to go deep immediately.
- 12
What is the time complexity of BFS in terms of vertices (V) and edges (E)?
Model answer
O(V+E).
Explanation
Like DFS, BFS visits each vertex once and examines each edge once, so its runtime is linear in the size of the graph.
- 13
Describe the process of insertion sort.
Model answer
Insertion sort iterates through the list, comparing each element with previous ones and swapping it leftwards until it is in the correct position relative to the sorted portion.
Explanation
At each step, the algorithm takes the current element and inserts it into the already sorted part of the list by shifting larger elements to the right.
- 14
What are the best-case and worst-case time complexities of insertion sort?
Model answer
Best-case: O(n) (list already sorted). Worst-case: O(n2) (list sorted in reverse order).
Explanation
In the best case, only one comparison per element is needed; in the worst case, each element must be compared with all preceding ones.
- 15
When is insertion sort a good choice compared to other sorting algorithms?
Model answer
For small lists or lists that are already mostly sorted.
Explanation
Insertion sort has linear time on nearly sorted data and low overhead, making it efficient for small or nearly sorted datasets.
- 16
What category of algorithm does merge sort belong to?
Model answer
Divide-and-conquer algorithm.
Explanation
Merge sort divides the list into halves, recursively sorts each half, and then merges the sorted halves back together.
- 17
What is the time complexity of merge sort in all cases (best, average, worst)?
Model answer
O(nlogn) for all cases.
Explanation
Merge sort always divides the list into halves (logn levels) and does O(n) work per level to merge, giving O(nlogn).
- 18
What is the space complexity of merge sort?
Model answer
O(n).
Explanation
Merge sort requires additional memory to store the temporary merged arrays, proportional to the size of the input list.
- 19
How does quick sort choose a pivot?
Model answer
Ideally, the pivot is chosen as close to the median of the list as possible.
Explanation
A good pivot near the median splits the list into two roughly equal halves, leading to the best performance. Common strategies include picking the first, last, or middle element, or a random element.
- 20
What are the best-case and worst-case time complexities of quick sort?
Model answer
Best-case: O(nlogn). Worst-case: O(n2).
Explanation
Best-case occurs when the pivot divides the list evenly each time; worst-case occurs when the pivot is always the smallest or largest element (e.g., already sorted list with bad pivot choice).
- 21
Why is quick sort often faster than merge sort in practice, despite the same average time complexity?
Model answer
Quick sort has an optimized inner loop and better cache locality, making it 2ā3 times faster on average. It also uses O(logn) extra space compared to merge sort's O(n).
Explanation
The inner loop of quick sort is simple and can be heavily optimized, and it works well with CPU caches. Additionally, in-place partitioning uses less memory.
- 22
What is the space complexity of quick sort?
Model answer
O(logn) (due to recursion stack).
Explanation
Quick sort sorts in place, requiring only a small amount of space for the recursive call stack, which is logarithmic in the size of the list on average.
- 23
Which sorting algorithm is harder to implement correctly: merge sort or quick sort?
Model answer
Quick sort is harder to implement correctly, as small inefficiencies can drastically slow it down.
Explanation
Merge sort has a straightforward divide-and-conquer structure, whereas quick sort requires careful pivot selection and partitioning logic to avoid worst-case behavior.
- 24
Define a greedy algorithm.
Model answer
A greedy algorithm makes the best possible choice at each local decision point, without considering the global picture.
Explanation
At every step, it chooses the option that appears optimal at that moment, hoping that locally optimal choices lead to a globally optimal solution.
- 25
Provide an example where a greedy algorithm fails to find the optimal solution.
Model answer
In a path-finding problem where you must minimize total cost, a greedy algorithm might choose the cheapest immediate step (e.g., 7 then 9) but miss a much cheaper path (e.g., 8 then 3) resulting in higher total cost.
Explanation
The example from the video shows a decision tree: greedy picks 7 (cost 7) then 9 (total 16), while the optimal path picks 8 then 3 (total 11, actually 8+3=11, but video said 3total?Wait,videosaid3 total? Actually video said
- 26
When are greedy algorithms particularly useful?
Model answer
When finding the exact optimal solution is computationally infeasible, such as for problems with an exponential number of possibilities.
Explanation
Greedy algorithms provide a fast, approximate solution that is often close to optimal, making them suitable for large-scale optimization problems.
- 27
What is the Traveling Salesman Problem (TSP)?
Model answer
The TSP asks for the shortest possible route that visits each city exactly once and returns to the starting city, given a list of cities and distances between them.
Explanation
It is a classic NP-hard problem where the number of possible routes grows factorially, making exact solutions impractical for more than a few cities.
- 28
Why is brute-force solution not feasible for TSP with 50 cities?
Model answer
The number of possible routes is (50ā1)!/2, which is astronomically large (over 6times1062), far beyond the capacity of modern computers.
Explanation
The factorial growth means even with 50 cities, the number of permutations is enormous, making exact search impossible.
- 29
How does a greedy algorithm approach the Traveling Salesman Problem?
Model answer
Start at an arbitrary city, and at each step, choose the nearest unvisited city. Repeat until all cities are visited, then return to the start.
Explanation
This nearest-neighbor heuristic is a greedy approach that makes locally optimal choices, producing a route that is usually much better than random but not necessarily optimal.

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