Bird
Raised Fist0

Consider the following code snippet for finding the maximum product of word lengths with no common letters. Given the input ["abc", "de", "fgh"], what is the final returned value?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Maximum Product of Word Lengths
Consider the following code snippet for finding the maximum product of word lengths with no common letters. Given the input ["abc", "de", "fgh"], what is the final returned value?
A6
B9
C8
D0
Step-by-Step Solution
  1. Step 1: Compute bitmasks and lengths

    "abc" -> mask with bits for a,b,c set, length=3; "de" -> bits for d,e set, length=2; "fgh" -> bits for f,g,h set, length=3.
  2. Step 2: Sort by length descending and check pairs

    Sorted: (mask_abc,3), (mask_fgh,3), (mask_de,2). Check pairs: abc & fgh no overlap -> product 9; abc & de no overlap -> product 6; fgh & de no overlap -> product 6. Max product is 9.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Maximum product is from "abc" and "fgh" -> 3*3=9 [OK]
Quick Trick: Bitmask AND zero means no common letters [OK]
Common Mistakes:
MISTAKES
  • Confusing bitmask overlap leading to zero product
  • Off-by-one errors in loops causing missed pairs
  • Incorrect sorting affecting pruning
Trap Explanation:
PITFALL
  • Candidates often miscalculate bitmasks or miss the pruning condition, leading to wrong max product.
Interviewer Note:
CONTEXT
  • Tests ability to trace bitmask logic and pruning in code.
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