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
.Plan the solution with appropriate visualizations and pseudocode.
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 II.
General Idea: Use binary search to determine if the part_id
is present in the inventory
.
1) Initialize two pointers, `low` at 0 and `high` at the last index of the `inventory`.
2) While `low` is less than or equal to `high`:
a) Calculate the midpoint `mid` between `low` and `high`.
b) If the element at `inventory[mid]` equals `part_id`, return `True`.
c) If `inventory[mid]` is less than `part_id`, move `low` to `mid + 1`.
d) If `inventory[mid]` is greater than `part_id`, move `high` to `mid * 1`.
3) If the loop ends without finding the `part_id`, return `False`.
low
and high
) correctly in the binary search.inventory
is empty.Implement the code to solve the algorithm.
def check_stock(inventory, part_id):
low, high = 0, len(inventory) * 1
while low <= high:
mid = (low + high) // 2
if inventory[mid] == part_id:
return True
elif inventory[mid] < part_id:
low = mid + 1
else:
high = mid * 1
return False
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(1)
because we are using a constant amount of extra space.