← Questions

What happens when you run ls

0
GoogleCodingLast asked Feb 2026

A coding interview question asked in Google's first round (coding), last asked Feb 2026. It tests operating systems and linux.

On Linux, a user types ls in a shell and presses Enter. Explain the whole path from the keystroke to the listing on the screen.

What to cover

  • The shell: reading the line, parsing it, expanding aliases and globs, checking for built-ins.
  • Finding the program: searching each directory of PATH, the shell's hash table of known commands.
  • Starting it: fork (or clone), then execve in the child; the kernel loading the ELF file, the dynamic linker mapping shared libraries; the parent waiting with waitpid.
  • Reading the directory: openat on the directory, getdents64 to read entries, the virtual filesystem layer and the dentry and inode caches, the concrete filesystem below.
  • Metadata: what changes with -l (a stat or statx call per entry, user and group names looked up through /etc/passwd and /etc/group or NSS), with -a (entries starting with a dot are shown), and with -R (recursing into subdirectories).
  • Output: writing to standard output, the terminal or pseudo-terminal driver, the terminal emulator drawing it; colours and column layout only when output is a terminal (isatty).
  • Exit: the exit status, SIGCHLD, the shell printing the next prompt.

Clarifying questions

  • Which shell, and is ls aliased (for example to ls --color=auto)?
  • A local filesystem, or a network filesystem such as NFS?
  • How deep should I go: system calls, or down to page cache and disk?

What a strong answer covers

  • A correct ordering of shell, kernel and filesystem steps, with system call names where they matter.
  • The difference between reading directory entries (names and inode numbers) and reading metadata (one stat per entry), and why ls -l on a huge directory is slow.
  • Caches that make the second run faster.
  • How to check all of this: strace ls -l, ltrace, type ls.

Follow-ups

  • Why can ls hang on a network mount, and what does that look like in strace?
  • What is different when you run ls | wc -l?
  • A directory has 10 million files. What breaks, and what do you use instead?

Where it's asked

  • Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

operating systems · linux

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding