Practice
max_profit after the loop finishes when prices = [3, 2, 6]?Solution
Step 1: Trace iteration i=1 with price=2
min_price updated from 3 to 2, max_profit remains 0.Step 2: Trace iteration i=2 with price=6
max_profit updated to max(0, 6 - 2) = 4.Final Answer:
Option D -> Option DQuick Check:
Max profit is 4 from buying at 2 and selling at 6 [OK]
- Confusing indices and off-by-one errors
- Updating max profit before min price
- Returning intermediate instead of final max profit
def maxProfit(prices, fee):
cash, hold = 0, -prices[0]
for price in prices[1:]:
cash = max(cash, hold + price - fee)
hold = max(hold, cash - price)
return cash
prices = [1, 3, 2]
fee = 1
print(maxProfit(prices, fee))What is the output of this code?Solution
Step 1: Initialize variables
cash = 0, hold = -1 (buy at price 1)Step 2: Iterate over prices
Price=3: cash = max(0, -1 + 3 - 1) = max(0,1) = 1; hold = max(-1, 1 - 3) = max(-1,-2) = -1 Price=2: cash = max(1, -1 + 2 - 1) = max(1,0) = 1; hold = max(-1, 1 - 2) = max(-1,-1) = -1Final Answer:
Option D -> Option DQuick Check:
Final cash is 1, representing max profit after fee [OK]
- Off-by-one in loop
- Forgetting fee subtraction
[2, 3, 2]?
def rob_linear(nums, start, end):
prev, curr = 0, 0
for i in range(start, end + 1):
prev, curr = curr, max(curr, prev + nums[i])
return curr
def rob(nums):
n = len(nums)
if n == 1:
return nums[0]
return max(rob_linear(nums, 0, n - 2), rob_linear(nums, 1, n - 1))
print(rob([2,3,2]))
Solution
Step 1: Trace rob_linear(nums, 0, 1)
nums[0]=2, nums[1]=3 Iteration i=0: prev=0, curr=0 -> prev=0, curr=max(0,0+2)=2 Iteration i=1: prev=2, curr=max(2,0+3)=3 Return 3Step 2: Trace rob_linear(nums, 1, 2)
nums[1]=3, nums[2]=2 Iteration i=1: prev=0, curr=0 -> prev=0, curr=max(0,0+3)=3 Iteration i=2: prev=3, curr=max(3,0+2)=3 Return 3 Max(3,3) = 3Final Answer:
Option C -> Option CQuick Check:
Both cases yield 3, final output is 3 [OK]
- Off-by-one in loop range
- Confusing which subarray to use
- Incorrect max calculation
jumps returned when the input is [2,1,1]?Solution
Step 1: Trace variables for input [2,1,1]
n=3, loop i in [0,1]. - i=0: furthest = max(0, 0+2) = 2; i == current_end (0), so jumps = 1, current_end = 2 - i=1: furthest = max(2, 1+1) = 2; i != current_end (2), no jump increment Loop ends.Step 2: Return jumps
jumps = 1 is returned, meaning minimum 1 jump to reach the end.Final Answer:
Option C -> Option CQuick Check:
One jump from index 0 to last index covers entire array [OK]
- Incrementing jumps too early or too late
- Off-by-one errors in loop range
- Misunderstanding when to update current_end
def rob_linear(nums, start, end):
prev, curr = 0, 0
for i in range(start, end + 1):
prev, curr = curr, max(curr, prev + nums[i])
return curr
def rob(nums):
n = len(nums)
if n == 1:
return nums[0]
return max(rob_linear(nums, 0, n - 2), rob_linear(nums, 1, n - 1))
Solution
Step 1: Identify loops in rob_linear
rob_linear runs a single loop from start to end, which is O(n) in worst case.Step 2: Count calls in rob
rob calls rob_linear twice, each over roughly n/2 elements, so total O(n) time.Step 3: Consider space usage
The DP uses only constant extra space (variables prev and curr) during computation.Final Answer:
Option A -> Option AQuick Check:
Two linear scans over n elements with constant space -> O(n) time and O(1) space [OK]
- Confusing recursion with DP
- Assuming nested loops cause O(n²)
- Ignoring that calls are sequential, not nested
