Overview
Understanding computer science fundamentals helps you write better code, debug efficiently, and make informed architectural decisions. These concepts form the foundation for system design, performance optimization, and debugging complex issues.Why This Matters: In interviews at top tech companies, 40-60% of system design questions require deep understanding of these fundamentals.
Operating Systems
Process vs Thread
Process
- Independent execution unit
- Own memory space (isolated)
- Higher overhead to create (~10ms)
- Inter-process communication needed
- Crash isolation (one crash doesn’t affect others)
Thread
- Lightweight execution unit
- Shared memory with parent process
- Lower overhead to create (~1ms)
- Direct memory sharing
- One thread crash can crash entire process
When to Use Processes vs Threads
| Scenario | Use Process | Use Thread |
|---|---|---|
| Isolation needed | ✅ Browser tabs | ❌ |
| Shared state | ❌ | ✅ Web server workers |
| CPU-bound tasks | ✅ Parallel processing | ✅ (with limitations) |
| I/O-bound tasks | ❌ | ✅ Database connections |
| Fault tolerance | ✅ Microservices | ❌ |
Memory Management
Stack vs Heap Memory
| Aspect | Stack | Heap |
|---|---|---|
| Allocation | Automatic (LIFO) | Manual (malloc/new) |
| Speed | Very fast | Slower (fragmentation) |
| Size | Limited (~1-8 MB) | Limited by RAM |
| Scope | Function-local | Global access via pointers |
| Thread Safety | Each thread has own stack | Shared, needs synchronization |
| Memory Errors | Stack overflow | Memory leaks, dangling pointers |
Concurrency Fundamentals
Deadlock Conditions (All 4 must be present)
- Mutual Exclusion: Resource can’t be shared
- Hold and Wait: Process holds resource while waiting for another
- No Preemption: Can’t force process to release resource
- Circular Wait: Circular chain of processes waiting
Race Condition
Key Concepts
| Concept | Description | Interview Relevance |
|---|---|---|
| Deadlock | Circular wait for resources | High |
| Race Condition | Unpredictable behavior from timing | Very High |
| Mutex | Binary lock (0 or 1) | High |
| Semaphore | Counter-based lock (0 to N) | High |
| Virtual Memory | Abstraction over physical memory | Medium |
| Context Switching | Saving/restoring process state | Medium |
| Page Fault | Accessing memory not in RAM | Medium |
| Thrashing | Excessive paging, system slowdown | Medium |
Process Scheduling Algorithms
| Algorithm | Description | Pros | Cons |
|---|---|---|---|
| FCFS | First Come First Served | Simple | Convoy effect |
| SJF | Shortest Job First | Optimal avg wait | Starvation possible |
| Round Robin | Time quantum rotation | Fair | High context switching |
| Priority | Higher priority first | Important tasks fast | Starvation |
| Multilevel Queue | Multiple queues with priority | Flexible | Complex |
Networking
OSI Model (Simplified)
TCP Three-Way Handshake
TCP vs UDP
TCP
- Connection-oriented (handshake)
- Reliable delivery (ACKs, retransmission)
- Ordered packets (sequence numbers)
- Flow control (sliding window)
- Congestion control
- Use: Web, Email, File transfer, SSH
UDP
- Connectionless (no handshake)
- Best-effort delivery (no guarantees)
- No ordering guarantee
- No flow/congestion control
- Lower latency, less overhead
- Use: Gaming, Streaming, DNS, VoIP
DNS Resolution Process
HTTP/1.1 vs HTTP/2 vs HTTP/3
| Feature | HTTP/1.1 | HTTP/2 | HTTP/3 |
|---|---|---|---|
| Connection | Multiple TCP | Single TCP, multiplexed | QUIC (UDP-based) |
| Head-of-line blocking | Yes | At TCP level | No |
| Header compression | No | HPACK | QPACK |
| Server push | No | Yes | Yes |
| Encryption | Optional | Practically required | Required (TLS 1.3) |
HTTP Methods & Status Codes
WebSocket vs HTTP
Database Fundamentals
ACID Properties
| Property | Description | Example |
|---|---|---|
| Atomicity | All or nothing | Bank transfer: debit AND credit succeed or both fail |
| Consistency | Valid state transitions | Balance can’t be negative, referential integrity |
| Isolation | Concurrent transactions don’t interfere | Read committed, serializable |
| Durability | Committed data persists | Write-ahead logging, survives crashes |
Transaction Isolation Levels
| Level | Dirty Read | Non-Repeatable Read | Phantom Read | Performance |
|---|---|---|---|---|
| Read Uncommitted | ✅ Possible | ✅ Possible | ✅ Possible | Fastest |
| Read Committed | ❌ Prevented | ✅ Possible | ✅ Possible | Fast |
| Repeatable Read | ❌ Prevented | ❌ Prevented | ✅ Possible | Medium |
| Serializable | ❌ Prevented | ❌ Prevented | ❌ Prevented | Slowest |
SQL vs NoSQL
SQL (Relational)
- Structured schema (tables, rows, columns)
- ACID compliant
- Complex queries (JOINs, aggregations)
- Vertical scaling (scale up)
- Examples: PostgreSQL, MySQL, Oracle
- Use: Transactions, Analytics, Complex relationships
NoSQL
- Flexible schema (documents, key-value, graphs)
- BASE (Eventually consistent)
- Simple queries (by key/ID)
- Horizontal scaling (scale out)
- Examples: MongoDB, Redis, Cassandra, Neo4j
- Use: Real-time, Big Data, Caching, Social graphs
NoSQL Types
| Type | Data Model | Use Case | Example |
|---|---|---|---|
| Key-Value | Key → Value | Caching, Sessions | Redis, DynamoDB |
| Document | JSON documents | Content management, Catalogs | MongoDB, Couchbase |
| Column-Family | Wide columns | Time-series, Analytics | Cassandra, HBase |
| Graph | Nodes + Edges | Social networks, Recommendations | Neo4j, Amazon Neptune |
Indexing Deep Dive
When NOT to Use Indexes
- High write tables: Each index slows down INSERT/UPDATE/DELETE
- Small tables: Full table scan may be faster
- Low cardinality columns: boolean, status with few values
- Frequently updated columns: Index maintenance overhead
Database Normalization
| Normal Form | Rule | Example Violation |
|---|---|---|
| 1NF | No repeating groups, atomic values | phone: "123,456,789" |
| 2NF | No partial dependencies | Non-key depends on part of composite key |
| 3NF | No transitive dependencies | zip → city → state |
| BCNF | Every determinant is a candidate key | More strict 3NF |
Computer Architecture
CPU Cache Hierarchy
Latency Numbers Every Programmer Should Know
| Operation | Time |
|---|---|
| L1 cache reference | 0.5 ns |
| Branch mispredict | 5 ns |
| L2 cache reference | 7 ns |
| Mutex lock/unlock | 25 ns |
| Main memory reference | 100 ns |
| Compress 1KB with Zippy | 3,000 ns (3 μs) |
| Send 1KB over 1 Gbps network | 10,000 ns (10 μs) |
| Read 4KB randomly from SSD | 150,000 ns (150 μs) |
| Round trip within datacenter | 500,000 ns (0.5 ms) |
| Read 1 MB sequentially from SSD | 1,000,000 ns (1 ms) |
| Disk seek | 10,000,000 ns (10 ms) |
| Read 1 MB sequentially from disk | 20,000,000 ns (20 ms) |
| Send packet CA → Netherlands → CA | 150,000,000 ns (150 ms) |
Distributed Systems Basics
CAP Theorem
In a distributed system, you can only guarantee 2 of 3: Consistency, Availability, Partition Tolerance
| Choice | Trade-off | Examples |
|---|---|---|
| CP | Sacrifice availability for consistency | MongoDB, HBase, Redis Cluster |
| AP | Sacrifice consistency for availability | Cassandra, DynamoDB, CouchDB |
| CA | Not practical (network partitions happen) | Traditional RDBMS (single node) |
Consistency Models
| Model | Description | Use Case |
|---|---|---|
| Strong | All reads see latest write | Banking, Inventory |
| Eventual | Reads eventually see latest write | Social media feeds |
| Causal | Respects cause-effect ordering | Messaging apps |
| Read-your-writes | User sees their own writes immediately | User profile updates |
Practice Questions
What happens when you type a URL in a browser?
What happens when you type a URL in a browser?
- Browser checks caches: Browser cache, OS cache, router cache
- DNS resolution: Resolve domain to IP address
- Check local DNS cache
- Query recursive DNS resolver
- Query root → TLD → authoritative nameserver
- TCP connection: 3-way handshake (SYN → SYN-ACK → ACK)
- TLS handshake (if HTTPS):
- Client Hello (supported ciphers)
- Server Hello + Certificate
- Key exchange
- Encrypted connection established
- HTTP request sent: GET / HTTP/1.1 with headers
- Server processes request:
- Load balancer routes to server
- Server processes and queries database
- Generates response
- HTTP response received: HTML content with status code
- Browser renders:
- Parse HTML → DOM tree
- Parse CSS → CSSOM
- Execute JavaScript
- Render pixels to screen
How does a database index work?
How does a database index work?
Indexes are data structures (usually B-trees) that maintain sorted references to rows.B-Tree Structure:
- Balanced tree with sorted keys
- Each node can have multiple children
- Leaf nodes contain pointers to actual rows
- Instead of scanning all rows O(n)
- Binary search through tree O(log n)
- Follow pointers to find matching rows
- Faster reads (logarithmic lookup)
- Slower writes (must update index)
- Additional storage space
Explain deadlock and how to prevent it
Explain deadlock and how to prevent it
Deadlock occurs when: Four conditions are ALL met (Coffman conditions):
- Mutual exclusion - resource can’t be shared
- Hold and wait - holding one resource, waiting for another
- No preemption - can’t force release
- Circular wait - A waits for B, B waits for A
- Lock ordering: Always acquire locks in same global order
- Lock timeout: Give up after waiting too long
- Deadlock detection: Detect and kill one transaction
- Try-lock: Non-blocking attempt, back off if fails
- Avoid nested locks: Minimize lock scope
Explain the difference between processes and threads
Explain the difference between processes and threads
Process:
- Independent execution unit with own memory space
- Isolated - crash doesn’t affect other processes
- Higher creation/switching overhead (~10ms)
- Communication via IPC (pipes, sockets, shared memory)
- Lightweight execution unit within a process
- Shared memory with parent and sibling threads
- Lower creation/switching overhead (~1ms)
- Direct memory sharing (but needs synchronization)
- Processes: Isolation needed, fault tolerance, security
- Threads: Shared state, I/O-bound tasks, lower overhead needed
What is the difference between TCP and UDP?
What is the difference between TCP and UDP?
TCP (Transmission Control Protocol):
- Connection-oriented (handshake required)
- Reliable (acknowledgments, retransmission)
- Ordered (sequence numbers)
- Flow control (sliding window)
- Congestion control
- Use: Web, email, file transfer, SSH
- Connectionless (no handshake)
- Unreliable (no guarantees)
- Unordered (packets may arrive out of order)
- No flow/congestion control
- Lower latency, less overhead
- Use: Gaming, video streaming, DNS, VoIP
Explain database transaction isolation levels
Explain database transaction isolation levels
Read Uncommitted: Can see uncommitted changes (dirty reads)Read Committed: Only see committed changes, but same query may return different resultsRepeatable Read: Same query returns same results within transaction, but new rows may appear (phantoms)Serializable: Transactions execute as if sequential - no anomalies, but slowestProblems prevented by level:
- Dirty read: See uncommitted data
- Non-repeatable read: Same query, different results
- Phantom read: New rows appear matching query
Quick Reference Card
Numbers to Remember
| Metric | Value |
|---|---|
| L1 cache | ~1 ns |
| L2 cache | ~10 ns |
| RAM | ~100 ns |
| SSD random read | ~150 μs |
| HDD seek | ~10 ms |
| Same datacenter RTT | ~0.5 ms |
| Cross-region RTT | ~100-150 ms |
Common Ports
| Port | Service |
|---|---|
| 22 | SSH |
| 80 | HTTP |
| 443 | HTTPS |
| 3306 | MySQL |
| 5432 | PostgreSQL |
| 6379 | Redis |
| 27017 | MongoDB |
Interview Tip: Be ready to explain these concepts with real-world examples. Interviewers love when you can relate theory to practical scenarios. Use the “explain like I’m 5” technique to show deep understanding.