Started with the ls -l approach because it felt natural, and the interviewer let me go down that road for a bit before asking what happens with filenames that have spaces.
Start by clarifying requirements (e.g., recursive vs non-recursive, symlinks, hidden files) and then present multiple bash approaches, from simple to robust. Emphasize the pitfalls of parsing ls output and demonstrate safe handling of filenames with spaces or newlines using null delimiters and tools like find, sort, and awk.
Pro tip: Mention that using `ls` for parsing is an anti-pattern because it's designed for human-readable output; instead, use `find -print0` with `sort -z` and `head -z` to safely handle any filename. Also, note that `du` can be used for disk usage but `stat` is better for actual file size.
Ask whether the search should be recursive, include hidden files, follow symlinks, and what 'largest' means (apparent size vs disk usage). This shows attention to detail and avoids incorrect assumptions.
Show a naive solution like `ls -lS | head -n 2 | tail -n 1` and explain why it's fragile: parsing ls output breaks with spaces, newlines, and special characters in filenames.
Demonstrate a safe method: `find . -type f -printf '%s %p\0' | sort -z -n -r | head -z -n 1 | cut -z -d' ' -f2-`. Explain how null delimiters prevent issues with spaces and newlines.
Mention using `du -b` with `sort -n` and `awk`, or a bash loop with `stat`, comparing performance and portability. Highlight that `find -printf` is GNU-specific, so for portability, use `stat` or `du` with null delimiters.
Conclude that avoiding ls parsing, using null delimiters, and testing with edge-case filenames (spaces, newlines, glob characters) is crucial. Offer to write a complete script if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.