Bird
Raised Fist0

The following code attempts to form the largest number from a list of integers. Which line contains a subtle bug that causes incorrect output on inputs like [0, 0, 0]?

medium🐞 Bug Identification Q14 of Q15
Greedy Algorithms - Largest Number (Arrange to Form Biggest)
The following code attempts to form the largest number from a list of integers. Which line contains a subtle bug that causes incorrect output on inputs like [0, 0, 0]?
ALine 8: Returning concatenated string without zero check
BLine 4-6: Custom comparator function
CLine 7: Sorting with custom comparator
DLine 3: Converting integers to strings
Step-by-Step Solution
Solution:
  1. Step 1: Trace output for input [0,0,0]

    After sorting, nums_str is ['0', '0', '0'], concatenation is '000'.
  2. Step 2: Identify missing check for all zeros

    Without checking if first element is '0', the function returns '000' instead of '0'.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Adding a check to return '0' if nums_str[0] == '0' fixes the bug [OK]
Quick Trick: Check if first string is '0' to handle all-zero case [OK]
Common Mistakes:
MISTAKES
  • Forgetting all-zero check
  • Incorrect comparator logic
  • Sorting without custom comparator
Trap Explanation:
PITFALL
  • The code looks correct but misses the edge case of all zeros, causing wrong output format.
Interviewer Note:
CONTEXT
  • Tests attention to edge cases and subtle output correctness
Master "Largest Number (Arrange to Form Biggest)" 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