Common Interview Questions¶
"What Happens When..." Questions¶
These questions test your ability to explain complex processes end-to-end.
What happens when you type a command in the terminal?¶
Complete answer covering fork/exec flow:
-
Shell reads input using
read()system call from stdin -
Shell parses command (user space parsing)
-
Shell calls fork() to create child process:
- Kernel creates new Process Control Block (PCB)
- Assigns new PID
- Copies parent's memory using Copy-on-Write
- Copies file descriptor table
- Returns PID to parent, 0 to child
-
Child process calls exec() to load command:
- Kernel opens executable file
- Verifies ELF header
- Replaces process image (code, data, stack)
- Loads program segments into memory
- Sets up arguments and environment
- Jumps to entry point
-
Kernel schedules child using CFS scheduler
-
Program executes, making system calls as needed
-
Program exits, calling
exit()system call:- Closes file descriptors
- Releases memory
- Sends SIGCHLD to parent
- Becomes zombie process
-
Parent receives SIGCHLD signal
-
Parent calls wait() to collect exit status:
- Kernel provides exit status
- Kernel frees child's PCB
- Child is fully terminated
-
Shell prints new prompt
Deep dive topics to mention:
- Copy-on-Write optimization
- File descriptor inheritance
- Zombie process prevention
- Signal handling
What happens when you type www.google.com in your browser?¶
Complete networking answer:
-
Browser checks cache for IP address
-
OS checks /etc/hosts
-
DNS Resolution:
- Query recursive resolver (ISP or 8.8.8.8)
- Resolver queries root servers → .com TLD → google.com authoritative
- Returns IP address (e.g., 142.250.185.46)
- Result cached with TTL
-
ARP Resolution (if on local network):
- Need gateway MAC address
- Send ARP request broadcast
- Gateway responds with MAC
-
TCP Connection (port 443 for HTTPS):
- SYN → SYN-ACK → ACK (three-way handshake)
- Connection established
-
TLS Handshake:
- Client Hello (supported ciphers, TLS version)
- Server Hello (chosen cipher, certificate)
- Client verifies certificate (CA chain)
- Key exchange (DH or RSA)
- Both derive session keys
- Encrypted communication ready
-
HTTP Request:
GET / HTTP/2orHTTP/3- Host: www.google.com
- Headers (User-Agent, Accept, etc.)
-
Server Processing:
- Load balancer receives request
- Routes to backend server
- Application processes
- Database queries if needed
-
HTTP Response:
- Status: 200 OK
- Headers (Content-Type, etc.)
- HTML body
-
Browser Rendering:
- Parse HTML (DOM tree)
- Parse CSS (CSSOM)
- Execute JavaScript
- Fetch additional resources (images, CSS, JS)
- Render page
-
Connection: Keep-alive or close
What happens when fork() is called?¶
-
System call entry: User space → kernel mode
-
Create new PCB: Allocate task_struct
-
Assign PID: Get next available PID
-
Copy process state:
- File descriptor table (increment reference counts)
- Signal handlers
- Current working directory
- Resource limits
-
Memory setup:
- Copy page tables (virtual memory mappings)
- Mark pages as Copy-on-Write
- Don't copy physical pages yet (optimization)
-
Copy kernel structures:
- Open file table pointers
- Credentials (UID, GID)
- Timers
-
Add to scheduler: Child is now runnable
-
Return:
- Parent gets child PID
- Child gets 0
-
Both processes continue from same point
What happens when malloc() is called?¶
For small allocation (< 128KB):
-
Check glibc free lists:
- Fast bins (recently freed small chunks)
- Small bins (exact size match)
- Large bins (size ranges)
-
If found in bins: Return quickly
-
If not found:
- Request more heap space from kernel
- Call
brk()orsbrk()to increase program break - Kernel extends data segment
-
Split chunk: If larger than requested
-
Return pointer: To usable memory (after metadata)
For large allocation (>= 128KB):
-
Call mmap() system call:
- Request anonymous memory mapping
- Page-aligned allocation
- Separate from main heap
-
Kernel allocates virtual memory:
- Updates page tables
- Doesn't allocate physical pages yet (lazy)
-
Return pointer
Internal glibc structures:
- Each chunk has header (size, flags, pointers)
- Bins organize free chunks
- Thread caching (tcache) for performance
What happens during a system boot?¶
(See Boot Process for detailed answer)
- Power on → BIOS/UEFI POST
- Boot loader (GRUB) loads kernel
- Kernel initialization
- Load initramfs
- Mount root filesystem
- Start init/systemd (PID 1)
- Start configured services
- Reach target (e.g., multi-user.target)
Technical Deep Dive Questions¶
Explain inodes¶
An inode is a data structure that stores file metadata:
- File type and permissions
- Owner (UID/GID)
- File size
- Timestamps (atime, mtime, ctime)
- Link count (hard links)
- Data block pointers (direct, indirect, double indirect, triple indirect)
Important: Does NOT contain filename! Filenames are in directory entries (dentries).
Relationship:
- Directory entry maps filename → inode number
- Inode contains metadata and points to data blocks
- Multiple dentries can point to same inode (hard links)
Explain the relationship between file descriptors and inodes¶
Three-level structure:
-
File Descriptor (per-process):
- Integer (0, 1, 2, ...)
- Index into process's FD table
- Points to file table entry
-
File Table Entry (system-wide):
- File offset (current position)
- Access mode (read/write/append)
- Reference count
- Points to inode
-
Inode (filesystem):
- File metadata
- Points to data blocks
Example:
- Process calls
open("file.txt")→ Returns FD 3 - FD 3 → File table entry → Inode 12345 → Data blocks
- Multiple FDs can share same file table entry (dup(), fork())
- Multiple file table entries can reference same inode (multiple opens)
How does Copy-on-Write work?¶
After fork():
-
Initial state: Child and parent share physical pages
-
Pages marked read-only: In both page tables
-
On write attempt:
- Page fault occurs
-
Kernel's page fault handler:
- Allocates new physical page
- Copies data to new page
- Updates writing process's page table
- Marks page as writable
- Resumes execution
-
Result: Only modified pages are copied
Benefits:
- Fast fork() (no immediate copying)
- Memory efficient (only copy what changes)
- Most processes fork then exec (never write)
Explain virtual memory¶
Virtual memory provides each process with its own address space:
Benefits:
- Isolation: Processes can't access each other's memory
- Larger address space: Process can use more than physical RAM
- Simplified programming: Continuous address space
How it works:
- MMU translates virtual → physical addresses
- Page tables map virtual pages to physical frames
- TLB caches recent translations
- Pages can be in RAM or swapped to disk
Virtual address (48-bit on x86-64):
Each index navigates page table levels to find physical page.
Explain TCP three-way handshake in detail¶
(See Protocols for complete answer)
sequenceDiagram
participant Client
participant Server
Client->>Server: SYN (seq=x)
Note right of Server: Create connection state
Server-->>Client: SYN-ACK (seq=y, ack=x+1)
Note left of Client: Connection state
Client->>Server: ACK (ack=y+1)
Note left of Client: ESTABLISHED
Note right of Server: ESTABLISHED
Why three-way?:
- Synchronize sequence numbers
- Prevent old duplicate connections
- Establish bidirectional communication
Practice More Questions¶
Process Management & System Calls
- What happens when you type a command (like
ls) in a shell and press enter? Explain the entire process flow including fork(), exec(), and related system calls. - What is the difference between a process and a thread?
- Explain what happens when you run
curl www.google.comfrom the perspective of Linux internals. - What is a Process Control Block (PCB) and what information does it contain?
- Explain the difference between fork() and exec() system calls.
- What is a zombie process and how do you prevent it?
Memory Management
- Explain the difference between virtual memory and physical memory.
- What are the different segments of process memory (text, data, heap, stack)? Describe each.
- How does malloc() work internally? Explain memory allocation mechanisms like sbrk() and mmap().
- What is the difference between user space and kernel space? Why can't we directly access kernel space?
- How does the page cache work?
- Explain page faults and how the kernel handles them.
File Systems & I/O
- What is an inode? What information does it store?
- What happens when you open a file? Trace the system calls involved.
- Explain the difference between hard links and soft (symbolic) links.
- What is /proc filesystem and what is it used for? Give examples of useful files in /proc.
- Explain file descriptors. What are stdin, stdout, and stderr?
- What happens when you delete a file that's still open?
Signals & IPC
- What are signals in Linux? Explain the difference between SIGTERM and SIGKILL.
- What is a zombie process? How do you handle it?
- Explain different inter-process communication (IPC) mechanisms (pipes, sockets, shared memory, etc.).
- What is the difference between mutex and semaphore?
- What happens when you press Ctrl+C in a terminal?
- Explain the HUP signal and its typical use case.
- What happens during a context switch?
Boot & System Architecture
- Explain the Linux boot process from power-on to login prompt.
- What is the role of the init system (systemd/init)?
- Explain context switching. What triggers it and what is its cost?
- What is a system call? How does control transfer from user space to kernel space?
Troubleshooting & Performance
- A process is consuming 100% CPU. How do you troubleshoot this?
- What is the difference between a crash and a panic?
- How would you use strace to debug a failing application?
- What tools would you use to identify memory leaks in a running process?
- Explain the meaning of load average in Linux. What does "1.0" mean?
- A disk is showing 100% utilization. Walk through your troubleshooting steps.
- Explain how DNS resolution works.