Practice
max_platforms after processing the second train (index 1)?Solution
Step 1: Sort trains by arrival time
Sorted trains: [(900, 910), (940, 1200), (950, 1120)]Step 2: Process trains up to index 1
After first train: heap=[910], max_platforms=1 Second train arrival=940, heap top=910 ≤ 940, pop 910 Push 1200, heap=[1200], max_platforms=max(1,1)=1 Since question asks after second train, max_platforms=1Final Answer:
Option B -> Option BQuick Check:
Heap size after second train is 1, max_platforms updated to 1 [OK]
- Not popping from heap before push
- Confusing max_platforms update timing
- Off-by-one in iteration
k. The task is to remove exactly k digits from the string so that the resulting number is the smallest possible. Which algorithmic approach guarantees an optimal solution efficiently?Solution
Step 1: Understand the problem constraints
The problem requires removing digits to minimize the resulting number, which suggests a greedy approach to decide which digits to remove as we scan the string.Step 2: Why greedy with stack works
The stack-based greedy approach maintains a monotonically increasing sequence by popping larger digits when a smaller digit is encountered, ensuring the smallest possible prefix at each step.Final Answer:
Option D -> Option DQuick Check:
Greedy stack approach is known optimal for this problem [OK]
- Assuming sorting digits works ignores digit order
- Thinking DP is needed for this greedy problem
- Trying brute force is too slow for large inputs
removeKdigits("1432", 2)?
def removeKdigits(num: str, k: int) -> str:
builder = []
for digit in num:
while k > 0 and builder and builder[-1] > digit:
builder.pop()
k -= 1
builder.append(digit)
while k > 0:
builder.pop()
k -= 1
result = ''.join(builder).lstrip('0')
return result if result else '0'
Solution
Step 1: Trace builder and k during iteration
Start with builder = [], k=2 - digit='1': builder=['1'], k=2 - digit='4': '4' > '1', append: builder=['1','4'], k=2 - digit='3': '3' < '4', pop '4', k=1; append '3': builder=['1','3'] - digit='2': '2' < '3', pop '3', k=0; append '2': builder=['1','2']Step 2: Remove remaining k and finalize
k=0, no more pops. Result = '12' after stripping leading zeros.Final Answer:
Option C -> Option CQuick Check:
Output matches expected smallest number after removing 2 digits [OK]
- Not popping enough digits when smaller digit appears
- Removing digits from front only
- Forgetting to strip leading zeros
def removeKdigits(num: str, k: int) -> str:
builder = []
for digit in num:
while k > 0 and builder and builder[-1] > digit:
builder.pop()
k -= 1
builder.append(digit)
while k > 0:
builder.pop()
k -= 1
result = ''.join(builder) # Bug here
return result if result else '0'
Solution
Step 1: Identify missing leading zero removal
The code joins builder into a string but does not strip leading zeros, which can cause incorrect output like "0012" instead of "12".Step 2: Why this is a bug
Leading zeros must be removed to get the correct smallest number representation; otherwise, the output is invalid.Final Answer:
Option B -> Option BQuick Check:
Adding.lstrip('0')fixes the bug [OK]
- Forgetting to strip leading zeros
- Removing digits incorrectly in the loop
- Not popping remaining digits when k > 0
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: Understand the condition for counting wiggles
The condition must allow last_diff to be zero or equal to zero to handle initial or equal consecutive elements correctly.Step 2: Identify the bug
Using strict inequalities excludes cases where last_diff is zero, causing the algorithm to skip valid wiggles after equal elements, leading to incorrect counts.Final Answer:
Option A -> Option AQuick Check:
Inclusive inequalities fix counting on equal consecutive elements [OK]
- Using strict inequalities causing missed wiggles
- Incorrect initialization of counters
- Updating last_diff outside condition
