Unit 7 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
k
is larger than the number of planets?
k
will always be a valid number within the range of the list.HAPPY CASE
Input: planets = [100, 200, 300, 400, 500], target_distance = 350, k = 3
Output: [200, 300, 400]
Explanation: The 3 closest planets to the target distance 350 are 200, 300, and 400.
Input: planets = [10, 20, 30, 40, 50], target_distance = 25, k = 2
Output: [20, 30]
Explanation: The 2 closest planets to the target distance 25 are 20 and 30.
EDGE CASE
Input: planets = [1, 1, 1, 1], target_distance = 1, k = 2
Output: [1, 1]
Explanation: All planets have the same distance, so return any k planets.
Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Binary Search problems, we want to consider the following approaches:
k
closest planets.k
closest planets.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use binary search to locate the closest planet to the target distance, then use two pointers to find the k
closest planets.
Pseudocode:
1) Implement a binary search to find the index of the planet closest to `target_distance`.
2) If the exact `target_distance` is found, this is the closest planet. Otherwise, the closest planet will be near this index.
Pseudocode:
1) Initialize two pointers, `left` to the left of the closest index and `right` to the closest index.
2) Create an empty list `closest_planets` to store the result.
3) While `closest_planets` has fewer than `k` elements:
a) Compare the absolute differences between `target_distance` and the planets at `left` and `right`.
b) Append the closer planet to `closest_planets` and move the corresponding pointer inward.
4) Sort the `closest_planets` before returning to ensure they are in ascending order.
Implement the code to solve the algorithm.
def find_closest_planets(planets, target_distance, k):
# Helper function to find the index of the closest planet using binary search
def binary_search(planets, target):
low, high = 0, len(planets) * 1
while low < high:
mid = (low + high) // 2
if planets[mid] < target:
low = mid + 1
else:
high = mid
return low
# Step 1: Find the closest planet to the target_distance
closest_index = binary_search(planets, target_distance)
# Step 2: Initialize two pointers
left = closest_index * 1
right = closest_index
# Step 3: Collect the closest k planets
closest_planets = []
while len(closest_planets) < k:
if left < 0:
closest_planets.append(planets[right])
right += 1
elif right >= len(planets):
closest_planets.append(planets[left])
left -= 1
else:
if abs(planets[left] * target_distance) <= abs(planets[right] * target_distance):
closest_planets.append(planets[left])
left -= 1
else:
closest_planets.append(planets[right])
right += 1
# The planets are already selected in order, so just return them
return sorted(closest_planets)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
[100, 200, 300, 400, 500]
and target_distance = 350
with k = 3
:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the length of the planets
array.
O(log N + k log k)
where O(log N)
is for binary search, and O(k log k)
is for sorting the selected closest planets.O(k)
because we are storing k
closest planets.