Bird
Raised Fist0

What is the output of the following code on the input costs = [[0,0],[0,0]]?

medium🧾 Code Trace Q4 of Q15
Greedy Algorithms - Two City Scheduling
What is the output of the following code on the input costs = [[0,0],[0,0]]?
A2
B0
C1
DError
Step-by-Step Solution
Solution:
  1. Step 1: Sort costs by difference (0 - 0 = 0)

    Costs remain [[0,0],[0,0]] after sorting.
  2. Step 2: Assign first n=1 to city A and second to city B, sum costs

    City A: 0; City B: 0; Total = 0 + 0 = 0
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sum of zero costs is zero [OK]
Quick Trick: Zero costs sum to zero regardless of assignment [OK]
Common Mistakes:
MISTAKES
  • Assuming non-zero output
  • Off-by-one errors in loop
  • Misunderstanding sorting with equal differences
Trap Explanation:
PITFALL
  • Candidates may think zero costs produce non-zero sums or cause errors.
Interviewer Note:
CONTEXT
  • Tests code trace on edge case with zero costs
Master "Two City Scheduling" in Greedy Algorithms

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Greedy Algorithms Quizzes