|
| 1 | +3451\. Find Invalid IP Addresses |
| 2 | + |
| 3 | +Hard |
| 4 | + |
| 5 | +Table: `logs` |
| 6 | + |
| 7 | + +-------------+---------+ |
| 8 | + | Column Name | Type | |
| 9 | + +-------------+---------+ |
| 10 | + | log_id | int | |
| 11 | + | ip | varchar | |
| 12 | + | status_code | int | |
| 13 | + +-------------+---------+ |
| 14 | + log_id is the unique key for this table. |
| 15 | + Each row contains server access log information including IP address and HTTP status code. |
| 16 | + |
| 17 | +Write a solution to find **invalid IP addresses**. An IPv4 address is invalid if it meets any of these conditions: |
| 18 | + |
| 19 | +* Contains numbers **greater than** `255` in any octet |
| 20 | +* Has **leading zeros** in any octet (like `01.02.03.04`) |
| 21 | +* Has **less or more** than `4` octets |
| 22 | + |
| 23 | +Return _the result table_ _ordered by_ `invalid_count`, `ip` _in **descending** order respectively_. |
| 24 | + |
| 25 | +The result format is in the following example. |
| 26 | + |
| 27 | +**Example:** |
| 28 | + |
| 29 | +**Input:** |
| 30 | + |
| 31 | +logs table: |
| 32 | + |
| 33 | + +--------+---------------+-------------+ |
| 34 | + | log_id | ip | status_code | |
| 35 | + +--------+---------------+-------------+ |
| 36 | + | 1 | 192.168.1.1 | 200 | |
| 37 | + | 2 | 256.1.2.3 | 404 | |
| 38 | + | 3 | 192.168.001.1 | 200 | |
| 39 | + | 4 | 192.168.1.1 | 200 | |
| 40 | + | 5 | 192.168.1 | 500 | |
| 41 | + | 6 | 256.1.2.3 | 404 | |
| 42 | + | 7 | 192.168.001.1 | 200 | |
| 43 | + +--------+---------------+-------------+ |
| 44 | + |
| 45 | +**Output:** |
| 46 | + |
| 47 | + +---------------+--------------+ |
| 48 | + | ip | invalid_count| |
| 49 | + +---------------+--------------+ |
| 50 | + | 256.1.2.3 | 2 | |
| 51 | + | 192.168.001.1 | 2 | |
| 52 | + | 192.168.1 | 1 | |
| 53 | + +---------------+--------------+ |
| 54 | + |
| 55 | +**Explanation:** |
| 56 | + |
| 57 | +* 256.1.2.3 is invalid because 256 > 255 |
| 58 | +* 192.168.001.1 is invalid because of leading zeros |
| 59 | +* 192.168.1 is invalid because it has only 3 octets |
| 60 | + |
| 61 | +The output table is ordered by invalid\_count, ip in descending order respectively. |
0 commit comments