Practice problem · Amazon · Medium

File Search (Unix find)

A search like Unix find: start at a folder, look at every file below it, and keep the files that pass the conditions you give it. 3 stages in Python written by you, with an assistant that only answers. Pro.

Stage 1 of 3: Find files by condition

Background. A search like Unix find: start at a folder, look at every file below it, and keep the files that pass the conditions you give it. tree.py holds the tree in memory as File and Folder objects; it is read-only.

Implement find(start, *conditions) and the conditions NameContains, Extension and LargerThan in finder.py.

  • find looks at every file in start and in every folder below it, at any depth, and returns the paths of the files that every condition matches. With no condition, every file. Folders are never in the result.
  • A path is the start folder's name, then the names of the folders down to the file, then the file's name, joined with /: home/docs/report.txt. The list is sorted the way Python's sorted orders strings, so home/a.txt comes before home/a/b.txt. Nothing found gives [].
  • start must be a Folder; anything else raises NotADirectoryError.
  • A condition is an instance of a subclass of Condition that defines matches(file), returning True or False. Anyone can write a new kind of condition that way, outside finder.py too, and find must work with it.
  • NameContains(text) matches a file whose own name contains text, case-sensitive. The empty text matches every file.
  • Extension(ext) (ext written without the leading dot: "txt", not ".txt") matches a file whose extension is exactly ext, case-sensitive. To find a name's extension, leave out any dots at the very start of the name; if a dot is left, the extension is everything after the last one, otherwise there is none. So a.tar.gz has gz, .config.json has json, A.TXT has TXT, notes. has the empty extension (which Extension("") matches), and .env and README have none. An extension never holds a dot, so an ext with one, such as 2.3, matches nothing.
  • LargerThan(n) matches a file whose size is greater than n bytes, so a size of exactly n does not match.

Example

home holds notes.txt (120 bytes), photo.jpg (9,000) and folder docs, which holds report.txt (4,000) and folder old with another report.txt (20).

find(home, Extension("txt"), LargerThan(100))
# ["home/docs/report.txt", "home/notes.txt"]

Notes

  • Read what a folder holds only through Folder.entries().
  • On the starter, all 14 tests in tests/test_part1.py fail.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: finder.py
  • Read-only: tree.py and all tests/*.py files
  • Trees are at most 200 folders deep and a few thousand entries; nothing is timed

What comes after stage 1

The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: And, or, not. Conditions combine with and, or and not, nested to any depth, including conditions written outside your code.
  2. Stage 3: Depth limits and skipped folders. Stop at a given depth and leave whole folders out, without ever looking inside what is left out.

The files you get

  • Yours to edit: finder.py.
  • Read-only: tree.py, tests/trees.py.
  • Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.

The round: no AI, an assistant that only answers

Amazon runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.

Frequently asked, briefly answered

Is File Search (Unix find) free?

It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.

Which company asks something like this?

Amazon. It is modelled on Amazon's coding round, which runs without AI. Based on candidates' reports of Amazon interviews. Nyrion wrote this version, so the question you get may differ.

Do I bring my own AI tool?

No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.

What does the interviewer ask?

6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.

Start this problem → Sign in

More like it: every practice problem, Amazon's interviews, the AI coding interview guide.