Google Interview Question for Site Reliability Engineers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
11
of 11 vote

You can observe that command at the system call layer by putting the command in a file (test.bash), and then running through strace:

$ strace -f -o /tmp/my.output bash test.bash

Some highlights:
- You will see your strace command running the bash executable via the exec syscall

execve("/bin/bash")

- The bash process will read the text file and interpret the commands.
- This bash process will create a pipe to read output from the subshell that $(command) creates

pipe([3,4])

.
- The bash proces will call fork() (clone in linux) to create a subprocess for the subshell

clone(...)

- Parent process does a blocking read on the first file descriptor of the pipe (waiting to read output from the subshell)

read(3,  <unfinished ...>

- The new child process will use the second fd in the pipe for it's new stdout

dup2(4, 1)

- That new child pid will look for ls in the system path, then call exec() to run ls in the subshell process

execve("/bin/ls", ["ls"], [/* 22 vars */]) = 0

- The ls program uses open() to open the current directory, then readdir() / getdents() to read the directory entries:

26849 openat(AT_FDCWD, ".", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
26849 getdents(3, /* 13 entries */, 32768) = 416
26849 getdents(3, /* 0 entries */, 32768) = 0

- Next, the ls command writes to stdout

write(1, "file1"..., 128) = 128

- This wakes up the parent process blocked on the pipe (since child fd 1 is stdout, which was dup2'd to the second pipe fd 4)
26848 <... read resumed> "file1"..., 128) = 128
- The parent process waits for the child to exit, in case it hasn't already

wait4(-1, 0x7fff2eeb2398, WNOHANG, NULL)

- ajfabbri May 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In plain english:

1. the system creates a subshell (via fork/exec of bash)
2. the stdout of the subshell is directed to the variable 'list'
3. ls is executed in the subshell
4. ls reads the dir_ent of the directory provided as an argument (or $PWD if none)
5. ls then returns this dir_ent listing to it's std out, and exits.
6. the subshell exits

- erik@bandpage.com January 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

This lists the files in the current directory.
Try this for a cleaner output

echo list=$(ls)

- wolfengineer April 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

% ls /usr/share/man/man2 | sed -e s/.2.gz//g | xargs man -s 2 -k | sort | grep -v 'unimplemented system calls'

- Gan April 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

This seems like not an algo interview question. From shell perspective, a new variable is created if list is not present. Command "ls" is executed and output is stored in list. If the list is present, then the old value is overridden. I am not sure what system calls are made or file descriptors are created.

- Victor April 30, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More