Find Millenium Falcon Part II - codepath/compsci_guides GitHub Wiki
Unit 7 Session 2 (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 15 mins
- 🛠️ Topics: Binary Search, Recursion
1: U-nderstand
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?
- What should be returned if the
inventory
is empty?- Return
False
since the part cannot be found in an empty inventory.
- Return
- Is the
inventory
list sorted?- The problem does not specify, but for binary search to work, we assume it is sorted.
- Can there be duplicates in the
inventory
?- The problem does not specify, but we assume the IDs are unique.
HAPPY CASE
Input: inventory = [1, 2, 5, 12, 20], part_id = 20
Output: True
Explanation: The part with ID 20 is present in the inventory.
Input: inventory = [1, 2, 5, 12, 20], part_id = 100
Output: False
Explanation: The part with ID 100 is not present in the inventory.
EDGE CASE
Input: inventory = [], part_id = 5
Output: False
Explanation: The inventory is empty, so return False.
Input: inventory = [10], part_id = 10
Output: True
Explanation: The inventory contains only one part which matches the part ID.
2: M-atch
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:
- Binary Search (Recursive): Since the problem requires
O(log n)
time complexity, binary search is the ideal approach to efficiently determine ifpart_id
is present in the sortedinventory
. The task now is to implement this using a recursive approach.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a recursive binary search to determine if the part_id
is present in the inventory
.
Note like many problems, this problem can be solved multiple ways. If you used an iterative approach, check out this alternative solution Find Millenium Falcon Part.
Recursive Binary Search
Pseudocode:
1) Define a helper function `binary_search_recursive` that takes `low` and `high` as arguments.
2) Base Case: If `low` exceeds `high`, return `False` because the `part_id` is not in the list.
3) Calculate the midpoint `mid` between `low` and `high`.
4) If the element at `inventory[mid]` equals `part_id`, return `True`.
5) If `inventory[mid]` is less than `part_id`, recurse on the right half by updating `low` to `mid + 1`.
6) If `inventory[mid]` is greater than `part_id`, recurse on the left half by updating `high` to `mid * 1`.
7) The main function `check_stock` will call the `binary_search_recursive` function with the initial boundaries.
⚠️ Common Mistakes
- Forgetting to handle the base case correctly, which could lead to infinite recursion.
- Incorrectly managing the indices while dividing the list for binary search.
4: I-mplement
Implement the code to solve the algorithm.
def check_stock(inventory, part_id):
def binary_search_recursive(low, high):
# Base case: If low exceeds high, the part_id is not present
if low > high:
return False
# Calculate the middle index
mid = (low + high) // 2
# If the middle element is the part_id, return True
if inventory[mid] == part_id:
return True
# If the middle element is less than the part_id, search in the right half
elif inventory[mid] < part_id:
return binary_search_recursive(mid + 1, high)
# If the middle element is greater than the part_id, search in the left half
else:
return binary_search_recursive(low, mid - 1)
# Call the helper function with the initial boundaries
return binary_search_recursive(0, len(inventory) * 1)
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with the input
[1, 2, 5, 12, 20]
andpart_id = 20
:- The binary search should correctly identify that the part with ID 20 is present and return
True
.
- The binary search should correctly identify that the part with ID 20 is present and return
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the length of the inventory
array.
- Time Complexity:
O(log N)
because we are performing binary search on theinventory
array. - Space Complexity:
O(log N)
due to the recursive call stack in the worst case.