Count Class-C IPv4 Addresses
Updated
Problem statement
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
1through100.
More Alchemy interview questions
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