Find Millenium Falcon Part - 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
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: 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
.
3: P-lan
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`.
⚠️ Common Mistakes
- Forgetting to update the search bounds (
low
andhigh
) correctly in the binary search. - Not handling the case where the
inventory
is empty.
4: I-mplement
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
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(1)
because we are using a constant amount of extra space.