Practice
total_cost returned when the input is [1, 8, 3, 5]?Solution
Step 1: Trace initial heap and first merge
Heapify sticks: [1,3,5,8]. Pop 1 and 3 -> cost=4, total_cost=4. Push 4 back -> heap: [4,5,8]Step 2: Trace second and third merges
Pop 4 and 5 -> cost=9, total_cost=13. Push 9 -> heap: [8,9]. Pop 8 and 9 -> cost=17, total_cost=30. Push 17 -> heap: [17]. Loop ends.Final Answer:
Option A -> Option AQuick Check:
Sum of merges: 4 + 9 + 17 = 30 [OK]
- Adding costs incorrectly or missing last merge
- Confusing heap order or popping wrong elements
- Off-by-one in loop iterations
n. What is the output when n = 332?
def monotoneIncreasingDigits(n: int) -> int:
digits = list(map(int, str(n)))
marker = len(digits)
for i in range(len(digits) - 1, 0, -1):
if digits[i] < digits[i - 1]:
digits[i - 1] -= 1
marker = i
for i in range(marker, len(digits)):
digits[i] = 9
return int(''.join(map(str, digits)))
print(monotoneIncreasingDigits(332))
Solution
Step 1: Trace the loop from right to left
Digits start as [3, 3, 2]. At i=2, digits[2]=2 < digits[1]=3, so digits[1] decrements to 2 and marker=2.Step 2: Set digits from marker to end to 9
Digits become [3, 2, 9]. Next, at i=1, digits[1]=2 < digits[0]=3, so digits[0] decrements to 2 and marker=1. Then digits from index 1 onward set to 9 -> [2, 9, 9].Final Answer:
Option A -> Option AQuick Check:
Final digits [2,9,9] form 299 which is largest monotone ≤ 332 [OK]
- Stopping after first decrement without rechecking previous digits
- Not setting trailing digits to 9
- Returning intermediate digits without full fix
count after the loop finishes when the input is [1, 5, 4]?
def wiggleMaxLength(nums):
if not nums:
return 0
count = 1
last_diff = 0
for i in range(1, len(nums)):
diff = nums[i] - nums[i - 1]
if (diff > 0 and last_diff <= 0) or (diff < 0 and last_diff >= 0):
count += 1
last_diff = diff
return count
Solution
Step 1: Trace loop iterations
Input: [1, 5, 4] - i=1: diff=5-1=4 >0 and last_diff=0 ≤0 -> count=2, last_diff=4 - i=2: diff=4-5=-1 <0 and last_diff=4 ≥0 -> count=3, last_diff=-1Step 2: Final count value
After loop, count=3, which is the length of the longest wiggle subsequence.Final Answer:
Option B -> Option BQuick Check:
Count increments twice for valid wiggles [OK]
- Off-by-one counting
- Ignoring initial count=1
- Not updating last_diff correctly
Solution
Step 1: Identify operations per digit
Each digit is pushed onto the stack once and can be popped at most once when a smaller digit arrives.Step 2: Analyze total operations
Since each push and pop happens at most once per digit, total operations are proportional to n.Final Answer:
Option A -> Option AQuick Check:
Stack operations are linear in n, no nested loops [OK]
- Assuming worst case k pops per digit -> O(n*k)
- Confusing with sorting complexity
- Thinking nested loops are needed
Solution
Step 1: Recognize negatives affect ordering and concatenation semantics
Negative numbers cannot be treated the same as positives because concatenation with '-' changes lex order.Step 2: Separate positives and negatives, sort positives with original comparator, sort negatives by absolute value descending
Concatenate positives first (largest number), then negatives to maintain largest overall concatenation.Step 3: This approach preserves ordering logic and handles negatives correctly
Other options either ignore negatives or mishandle signs causing incorrect results.Final Answer:
Option C -> Option CQuick Check:
Separating and sorting by sign handles negatives correctly [OK]
- Treating negatives as strings directly
- Ignoring negatives
- Converting negatives to positives incorrectly
