Problem
Table: logs
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| log_id | int |
| ip | varchar |
| status_code | int |
+-------------+---------+
log_id is the unique key for this table.
Each row contains server access log information including IP address and HTTP status code.
Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:
- Contains numbers greater than
255
in any octet - Has leading zeros in any octet (like
01.02.03.04
) - Has less or more than
4
octets
Return the result table ordered by invalid_count
, ip
indescending order respectively.
The result format is in the following example.
Example 1:
|
|
Example 2:
Solution
Method 1 – Pattern Matching and Aggregation
Intuition
To find invalid IPv4 addresses, we need to check for three conditions: any octet > 255, any octet with leading zeros, or not exactly 4 octets. We can use SQL string and pattern functions or pandas string methods to check these conditions for each IP.
Approach
- Split the IP into octets using string functions.
- Check:
- If the number of octets is not 4.
- If any octet has leading zeros (length > 1 and starts with ‘0’).
- If any octet is not a number or is > 255.
- Count the number of invalid occurrences for each IP.
- Return the IP and its invalid count, ordered by invalid_count and ip descending.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, where n is the number of log rows. Each IP is checked once. - 🧺 Space complexity:
O(n)
, for storing the result and intermediate groupings