FastPrepCount Class-C IPv4 Addresses
Problems/Count Class-C IPv4 Addresses

Count Class-C IPv4 Addresses

Alchemy logoAlchemy● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A first-hand report from an Alchemy SRE phone screen asks you to get a list of IPs and work out how many are class-C and how many there are in total. Our version keeps just the parsing and counting task, with a fixed answer for each input, and says clearly what to do with bad input. It is about 90% the same as the reported question.

You are given addresses, a list of strings returned by an IP-list endpoint. Count how many of them are valid class-C IPv4 addresses, and how many are valid IPv4 addresses of any class.

An IPv4 address has exactly four parts called octets. Each octet is a decimal number from 0 through 255. An octet may not have a 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]: the class-C count first, then the count of all valid addresses. Skip any malformed string, meaning one that is not a valid IPv4 address.

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 addresses are valid, and their first octets are in the class-C range. The 10.x address is valid but not class C. The last address is not valid because its first octet is too large. That gives two class-C addresses and three valid addresses.

Example 2

addresses = ["01.2.3.4","224.0.0.1","192.0.2.1"]return = [1,2]

The first string is not valid because its first octet has a leading zero. The second address is valid, but a first octet of 224 starts class D, so it is not class C. The third address starts with 192, so it is valid and class C. That gives one class-C address and two valid addresses.

Constraints

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

More Alchemy interview questions

  • Equal-Sum Digit PartitionsOA · Seen May 2022

Similar questions at other companies

  • Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Count Repeat Customers Across DatesAmazon · PHONE SCREEN
  • Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
  • Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
  • Reversible Run-Length EncodingZillow · PHONE SCREEN
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Parse and Order Structured LogsCoinbase · ONSITE INTERVIEWCoinbase interview questions
  • Rank Word-Translation Hints from Sentence TranslationsDuolingo · ONSITE INTERVIEWDuolingo interview questions
  • Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
See Alchemy hiring insights
Saved
class Solution {
    public int[] countClassCAddresses(String[] addresses) {
        // write your code here
    }
}
Checking account…