Bird
Raised Fist0

The following code attempts to find the maximum product of word lengths with no common letters. Which line contains a subtle bug that can cause incorrect results or inefficiency?

medium🐞 Bug Identification Q14 of Q15
Bit Manipulation - Maximum Product of Word Lengths
The following code attempts to find the maximum product of word lengths with no common letters. Which line contains a subtle bug that can cause incorrect results or inefficiency?
ALine 18: Checking bitmask overlap with bitwise AND
BLine 10: Sorting masks by length in descending order
CLine 14: Breaking outer loop when max product cannot improve
DLine 6: Incorrect bit shift by adding 1 to character index
Step-by-Step Solution
  1. Step 1: Identify bitmask construction

    Line 6 shifts bits by (ord(ch) - ord('a') + 1), which incorrectly shifts by 1 extra, causing wrong bitmask representation.
  2. Step 2: Impact of incorrect bitmask

    This causes overlap checks to fail or miss common letters, leading to incorrect max product or missed pruning opportunities.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Bitmask must shift by (ord(ch) - ord('a')) exactly [OK]
Quick Trick: Bit shift must match character index exactly [OK]
Common Mistakes:
MISTAKES
  • Off-by-one bit shifting in bitmask
  • Not pruning pairs after sorting
  • Incorrect overlap check logic
Trap Explanation:
PITFALL
  • The off-by-one bit shift looks plausible but breaks overlap detection subtly.
Interviewer Note:
CONTEXT
  • Tests attention to detail in bitmask construction and subtle bugs.
Master "Maximum Product of Word Lengths" in Bit Manipulation

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 Bit Manipulation Quizzes