Practice
1. You need to ship packages within a given number of days. Each package has a weight, and you must ship packages in order without splitting any package. You want to find the minimum ship capacity to ship all packages within the deadline. Which algorithmic approach guarantees finding the optimal ship capacity efficiently?
easy
Solution
Step 1: Understand the problem constraints
The problem requires finding a minimum capacity that allows shipping all packages in order within a fixed number of days.Step 2: Recognize the pattern
Binary searching the capacity (answer space) and checking feasibility is the optimal approach because capacity ranges between max package weight and sum of all weights, and feasibility can be checked in linear time.Final Answer:
Option D -> Option DQuick Check:
Binary search on answer space is classic for optimization with feasibility checks [OK]
Hint: Binary search on capacity range solves this optimally [OK]
Common Mistakes:
- Thinking greedy alone finds minimal capacity
- Confusing DP with subset sums
- Using two pointers without sorting relevance
2. You are given a sorted array and a target number
x. The task is to find k elements closest to x in the array, with ties broken by choosing the smaller elements first. Which of the following approaches guarantees an optimal solution with logarithmic search complexity on the array?easy
Solution
Step 1: Understand problem constraints
The array is sorted, and we wantkclosest elements toxwith tie-breakers favoring smaller elements.Step 2: Identify approach with logarithmic complexity
Binary search on the left boundary of the window of sizekefficiently narrows down the candidate window inO(log(n-k))time, guaranteeing optimality.Final Answer:
Option C -> Option CQuick Check:
Binary search exploits sorted order and window size constraints [OK]
Hint: Binary search on window boundaries exploits sorted array [OK]
Common Mistakes:
- Assuming greedy selection always yields optimal window
- Using DP unnecessarily increasing complexity
- Two-pointer approach is linear, not logarithmic
3. You are given a sorted array and a target value. You need to find the index where the target should be inserted to maintain the sorted order. Which algorithmic approach guarantees an optimal solution for this problem?
easy
Solution
Step 1: Understand problem constraints
The array is sorted, so searching can be optimized by halving the search space instead of scanning linearly.Step 2: Identify the algorithm that efficiently finds insertion index
Binary search divides the array repeatedly, guaranteeing O(log n) time, which is optimal compared to linear or DP approaches.Final Answer:
Option D -> Option DQuick Check:
Binary search is the classic approach for sorted array search problems [OK]
Hint: Sorted array + insertion -> binary search [OK]
Common Mistakes:
- Assuming greedy insertion is always optimal
- Confusing DP with binary search
- Using linear scan despite sorted input
4. The following code attempts to find the k-th smallest number in an m x n multiplication table. Which line contains a subtle bug that can cause incorrect results for large inputs?
medium
Solution
Step 1: Identify counting logic
The counting function must cap counts per row by n, since each row has only n elements.Step 2: Analyze line 6
Line 6 adds x // i without min(n, x // i), causing overcounting when x // i > n, leading to incorrect binary search decisions.Final Answer:
Option B -> Option BQuick Check:
Bug causes wrong counts and incorrect final answer [OK]
Hint: Count per row capped by n to avoid overcounting [OK]
Common Mistakes:
- Forgetting min(n, x//i) in counting
- Incorrect mid calculation
5. The following code attempts to find the maximum minimum distance to place
m balls in position. Identify the bug that causes incorrect results or infinite loops.medium
Solution
Step 1: Check sorting necessity
The feasibility check assumes positions are sorted to place balls greedily. Missingposition.sort()breaks this assumption.Step 2: Consequences of missing sort
Without sorting, the greedy check may fail to place balls correctly, causing wrong feasibility results and incorrect final answer.Final Answer:
Option D -> Option DQuick Check:
Sorting is essential before greedy feasibility check [OK]
Hint: Positions must be sorted before greedy feasibility check [OK]
Common Mistakes:
- Forgetting to sort positions
- Incorrect binary search boundaries causing infinite loops
