Reported July 2024
Alchemystring

Count Class-C IPv4 Addresses

Reported by candidates from Alchemy's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Alchemy OA. Under 2s to a working solution.
Founder's read

The Alchemy OA reported in July 2024 looks like an IP networking question, but it's really string validation with a counter bolted on. You get a list of strings, you decide which ones are valid IPv4, and you tally two numbers. No clever data structure, no optimization. The whole game is getting the validity rules exactly right under pressure. If you've got the OA coming up, expect this to be quick when you're careful and painful when you rush the edge cases. StealthCoder sits invisibly as a safety net on the live OA if you blank on the parsing details, but the logic here is short enough to own.

The problem

Given strings returned by an IP-list endpoint, count valid class-C IPv4 addresses and all valid IPv4 addresses.
An IPv4 address has exactly four decimal octets from 0 through 255, with no leading zero unless the octet is exactly 0. Under classful addressing, a valid address is class C when its first octet is from 192 through 223 inclusive.
Return [classCCount, validIPv4Count]. Ignore malformed strings.

Function
countClassCAddresses(addresses: String[]) → int[]

Examples
Example 1
addresses = ["192.168.1.1","223.0.0.1","10.0.0.1","256.1.1.1"]
return = [2,3]
The first two valid addresses are class C; the 10.x address is valid but not class C.
Example 2
addresses = ["01.2.3.4","224.0.0.1","192.0.2.1"]
return = [1,2]
Leading zeroes are invalid, 224 starts class D, and 192 starts class C.

Constraints
0 <= addresses.length <= 100000.
Each string has length from 1 through 100.

Reported by candidates. Source: FastPrep

Pattern and pitfall

What it reduces to: split each string on dots, validate four parts, then check the first octet. One pass over the array, so it's O(n) with a tiny constant since each string is at most 100 characters. The trick is a strict validator. Require exactly four parts. Each part must be non-empty, digits only, no leading zero unless the part is exactly "0", and its value must be 0 through 255. Check digits before converting to a number, or you'll crash or accept junk like "+1" or " 1". Watch trailing dots, which a lazy split can swallow in some languages. Then if valid, increment the valid count, and if the first octet is 192 through 223, increment the class C count. Return class C first, valid second. Swapping the order is the classic slip. StealthCoder is your hedge on the live OA if a validation edge case slips past you.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Count Class-C IPv4 Addresses cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Alchemy's OA.

Alchemy reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Class-C IPv4 Addresses FAQ

How hard is the Alchemy class-C IPv4 question really?+

Easy on algorithm, easy to fumble on details. It's a single pass with string parsing. Most lost points come from validation edge cases like leading zeros, empty octets, and non-digit characters, not from complexity. Write a clean helper function and test it against the two examples.

What's the trick to counting valid IPv4 addresses?+

Validate each octet strictly before doing math. Check it's non-empty, all digits, has no leading zero unless it's exactly "0", and falls between 0 and 255. Require exactly four parts after splitting on dots. Anything else gets ignored, as the problem states.

What edge cases should I test before submitting?+

Test "01.2.3.4" for leading zeros, "256.1.1.1" for out of range, and boundaries 192 and 223 versus 191 and 224. Also try too few or too many octets, a trailing dot, empty strings between dots, and letters or signs inside an octet. Also try an empty input array.

Does the return order matter?+

Yes. Return [classCCount, validIPv4Count], with class C first. Every class C address is also valid, so the first number can never exceed the second. If your output violates that, you've swapped them or counted class C without validating.

How do I prepare for this in 48 hours?+

Skip broad grinding. Practice a few string-parsing and validation problems, especially ones with strict format rules. Write a validator for this exact problem from scratch, run both examples, and add your own edge cases. This pattern is mostly careful implementation, so accuracy beats speed.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Alchemy.

OA at Alchemy?
Invisible during screen share
Get it