← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Amazon SWE coding round, got a filesystem traversal problem that looked deceptively simple at first glance. Took a while to get the regex right for the IP validation part.

Questions Asked (1)

Q1

Given a path to a directory, write a program that recursively scans all files under it, extracts all valid IPv4 addresses from the file contents, and prints each unique address once in lexicographic order. A valid IPv4 address has four dot-separated octets each between 0 and 255, with no leading zeros unless the octet is exactly zero.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The recursion part was fine, that's just os.walk or equivalent.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and edge cases, then outline a solution that recursively traverses the directory, reads each file, extracts IPv4 addresses using a robust regex or manual parsing, and stores them in a set for uniqueness. Finally, sort the set lexicographically and print each address. Discuss trade-offs such as memory usage, handling large files, and error handling.

Pro tip: Mention that you would use a streaming approach for large files to avoid loading entire files into memory, and discuss how to handle symlinks and permission errors gracefully. Also, emphasize the importance of defining 'lexicographic order' for IP addresses, which may differ from numeric order.

1. Clarify Requirements and Edge Cases

Ask about directory structure, file types, handling of symlinks, permission errors, and whether the output should be sorted lexicographically as strings or numerically. Confirm the definition of a valid IPv4 address.

2. Design the Recursive Traversal

Choose between os.walk (Python) or equivalent for recursive directory scanning. Discuss handling of nested directories, symbolic links, and potential infinite loops.

3. Extract IPv4 Addresses

Use a regex pattern that matches valid IPv4 addresses, ensuring octets are 0-255 and no leading zeros. Alternatively, parse manually by splitting on dots and validating each octet.

4. Store Unique Addresses and Sort

Use a set to store unique addresses, then convert to a list and sort lexicographically. Discuss memory implications and potential need for external sorting if the set is too large.

5. Print and Handle Errors

Print each address on a new line. Implement error handling for file read errors, permission issues, and invalid encodings. Consider logging or skipping problematic files.

Key Points to Mention

  • Use of regular expressions with word boundaries to avoid partial matches (e.g., '192.168.1.1' in '192.168.1.123').
  • Validation of octets: each must be 0-255 and no leading zeros (e.g., '01' is invalid).
  • Efficiency considerations: streaming file reads, using generators, and avoiding loading entire files into memory.
  • Handling of large directories and files: potential need for parallel processing or external sorting.
  • Error handling: dealing with permission errors, non-text files, and symlinks to prevent crashes.
  • Lexicographic vs numeric sorting: clarify that lexicographic order sorts as strings, which may not be intuitive for IP addresses.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.