Missing country code

Missing Country Code
Imagine you get a data set from a client that contains addresses from 150 countries all around the world and your task is to verify them, the data is stored in 4 fields – Address Line 1, Address Line 2, City, ZIP code. What you also have available is address verification solution for each country, but the data set does not include the country code. How would you design the logic that would process the data and use the verification components in the most efficient way?
Hint: Running all of those 150 address verification components against each record is not considered efficient.

💡Strategies for Solving This Problem

Finding Missing Element

This is usually "find missing number in array" in disguise. Common variation: array has numbers 1 to n, but one is missing. Find it.

The Setup

Imagine you have phone numbers with country codes 1 to n. One country code is missing from your dataset. Which one?

Approach 1: Sum Formula

Sum of 1 to n = n(n+1)/2

Calculate expected sum, subtract actual sum. The difference is the missing number.

O(n) time, O(1) space. Clean and simple.

Approach 2: XOR

XOR has property: a ⊕ a = 0 and a ⊕ 0 = a

XOR all numbers 1 to n, then XOR all array elements. The result is the missing number.

O(n) time, O(1) space. No overflow risk (unlike sum).

Approach 3: Hash Set

Add all array elements to set. Check which number from 1 to n is not in set.

O(n) time, O(n) space. Works but uses more memory.

Why XOR is Better

Sum formula can overflow if n is large. XOR doesn't have this problem.

XOR is also more versatile - works for finding duplicate, missing, or single number problems.

At Various Companies

This shows up all the time with different stories: missing file number, missing ID, missing floor number, etc. The solution is always the same.

Key question: What if there are multiple missing numbers? Or one duplicate? Each variation has a trick.

Scroll to Top