Bird
Raised Fist0

Given the following code snippet for UTF-8 validation, what is the return value when input is [197, 130, 1]?

easy🧾 Code Trace Q3 of Q15
Bit Manipulation - UTF-8 Validation
Given the following code snippet for UTF-8 validation, what is the return value when input is [197, 130, 1]?
ARaises an exception
BFalse
CTrue
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Trace first byte 197 (0b11000101)

    Leading bits 110 -> remaining = 1 (expect 1 continuation byte)
  2. Step 2: Check second byte 130 (0b10000010)

    Starts with 10 -> valid continuation byte, remaining decremented to 0
  3. Step 3: Check third byte 1 (0b00000001)

    Starts with 0 -> single byte character, remaining stays 0
  4. Step 4: End of data, remaining == 0 -> valid UTF-8

  5. Final Answer:

    Option C -> Option C
  6. Quick Check:

    All bytes valid and remaining zero at end [OK]
Quick Trick: Check continuation byte count matches [OK]
Common Mistakes:
MISTAKES
  • Forgetting to decrement remaining
  • Misreading leading bits
Trap Explanation:
PITFALL
  • Candidates often miss decrementing remaining or misinterpret leading bits causing false negatives.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute bitmasking logic on typical input.
Master "UTF-8 Validation" 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