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.
findlooks at every file instartand 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'ssortedorders strings, sohome/a.txtcomes beforehome/a/b.txt. Nothing found gives[]. startmust be aFolder; anything else raisesNotADirectoryError.- A condition is an instance of a subclass of
Conditionthat definesmatches(file), returningTrueorFalse. Anyone can write a new kind of condition that way, outsidefinder.pytoo, andfindmust work with it. NameContains(text)matches a file whose own name containstext, case-sensitive. The empty text matches every file.Extension(ext)(extwritten without the leading dot:"txt", not".txt") matches a file whose extension is exactlyext, 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. Soa.tar.gzhasgz,.config.jsonhasjson,A.TXThasTXT,notes.has the empty extension (whichExtension("")matches), and.envandREADMEhave none. An extension never holds a dot, so anextwith one, such as2.3, matches nothing.LargerThan(n)matches a file whose size is greater thannbytes, so a size of exactlyndoes 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.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
finder.py - Read-only:
tree.pyand alltests/*.pyfiles - 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.
- Stage 2: And, or, not. Conditions combine with and, or and not, nested to any depth, including conditions written outside your code.
- 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.
More like it: every practice problem, Amazon's interviews, the AI coding interview guide.