ITPEC FE Subject B October 2024 Question 16
ITPEC FE Subject B October 2024 — Question 16 of 20
Hamming Distance (extended) — fill in blanks for a function that computes Hamming distance between two strings of possibly different lengths.
The function compares characters up to the shorter string's length, then adds the length difference as extra mismatches.
- •Blank A: `length2 < length1` —
minLengthstarts aslength1; overwrite it withlength2only whenlength2is smaller (standard min-finding pattern). - •Blank B: `is not equal to` — Hamming distance counts differing positions, so increment
distancewhen characters do not match. - •Blank C: `distance + remainingLength` — after the loop,
distanceholds mismatches in the overlapping part;remainingLength(|length1 - length2|) accounts for extra unmatched characters.
Verification with example:hammingDistance("101010", "111000111"):
- minLength = 6, differences at positions 2 and 5 → distance = 2
- remainingLength = 9 - 6 = 3
- Result: 2 + 3 = 5 ✓
Why not others:
- (a) remainingLength alone ignores mismatches in the overlapping part
- (b) uses is equal to, which counts matches instead of differences
- (c) distance + minLength adds the overlap length instead of the extra length — wrong value
- (e, f) length2 > length1 flips the condition — minLength would become the max, causing index-out-of-bounds
- (g, h) same flipped condition as (e, f)
Key rule: Hamming distance = mismatches in overlapping portion + length difference for the extra characters.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.