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?
inventory
is empty?
False
since the part cannot be found in an empty inventory.inventory
list sorted?
inventory
?
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.
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:
O(log n)
time complexity, binary search is the ideal approach to efficiently determine if part_id
is present in the sorted inventory
. The task now is to implement this using a recursive approach.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.
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.
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)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
[1, 2, 5, 12, 20]
and part_id = 20
:
True
.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the length of the inventory
array.
O(log N)
because we are performing binary search on the inventory
array.O(log N)
due to the recursive call stack in the worst case.