Start with a clear, concise definition of both processes and threads, highlighting that a process is an independent execution unit with its own memory space, while a thread is a lighter unit of execution within a process that shares memory. Then, discuss the trade-offs in terms of resource usage, communication, and context switching, and relate them to real-world scenarios like server design or concurrency in applications.
Pro tip: Mention that while threads share memory and are cheaper to create, they introduce complexity like race conditions and require synchronization, which is crucial for high-performance systems like TikTok's backend. Also, note that modern architectures often use a hybrid approach (e.g., thread pools, async I/O) to balance overhead and scalability.
Clearly define what a process and a thread are, emphasizing that a process is an independent program in execution with its own address space, while a thread is a unit of execution within a process that shares the process's resources.
Compare them across dimensions such as memory space, resource ownership, creation overhead, context switching cost, and communication mechanisms.
Explain the trade-offs: processes offer isolation and stability but are heavier; threads offer lightweight concurrency and faster communication but require careful synchronization to avoid issues like race conditions.
Connect the concepts to system design decisions, such as when to use multi-processing vs. multi-threading, and mention patterns like thread pools, worker processes, or async I/O.
Conclude with a concrete example, such as a web server handling requests: using multiple processes for isolation or multiple threads for efficiency, and how TikTok might leverage these concepts for scalability.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by defining a thread pool as a collection of pre-created worker threads that process tasks from a shared queue, then walk through the lifecycle of a task from submission to execution. Explain the worker thread lifecycle and how the pool handles saturation through queuing and rejection policies. Conclude with trade-offs and real-world considerations, especially for high-throughput systems like TikTok.
Pro tip: Mention that thread pool tuning is workload-specific: CPU-bound tasks benefit from a pool size near the number of cores, while I/O-bound tasks need more threads or async I/O. Also, note that TikTok's massive scale demands careful backpressure and monitoring to avoid cascading failures.
Explain that a thread pool manages a set of reusable worker threads to execute tasks, reducing the overhead of thread creation and improving resource utilization.
Detail how tasks are submitted to a blocking queue (e.g., LinkedBlockingQueue) and how idle worker threads pick them up, ensuring FIFO or priority-based ordering.
Cover how worker threads are created, run in a loop taking tasks from the queue, and terminate based on keep-alive time or pool shutdown.
Describe what happens when the queue is full and all threads are busy: the pool may reject tasks using policies like AbortPolicy, CallerRunsPolicy, DiscardPolicy, or DiscardOldestPolicy.
Highlight key parameters (core pool size, max pool size, queue capacity, keep-alive) and how they affect latency, throughput, and resource usage, with examples from high-scale systems.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Mutual exclusion, hold-and-wait, no preemption, circular wait.
Start by clearly naming the four Coffman conditions (mutual exclusion, hold and wait, no preemption, circular wait) and briefly explain each. Then discuss prevention strategies by breaking at least one condition, and optionally mention avoidance (e.g., Banker's algorithm) or detection/recovery. Tailor your answer to TikTok's scale by emphasizing practical techniques like lock ordering and timeout mechanisms.
Pro tip: Show maturity by acknowledging that prevention often trades off concurrency and performance; in real systems, you might choose detection and recovery or avoidance instead. Mention specific tools or patterns you've used (e.g., lock ordering in Java, std::lock in C++) to demonstrate hands-on experience.
List mutual exclusion, hold and wait, no preemption, and circular wait. Briefly define each in one sentence.
For each condition, describe a technique to break it: e.g., avoid mutual exclusion with read-only resources, request all resources upfront to break hold and wait, allow preemption with timeouts, and impose a global lock order to break circular wait.
Mention that prevention can reduce concurrency and throughput. Introduce avoidance (Banker's algorithm) and detection/recovery as alternatives, noting when each is appropriate.
Give examples from operating systems or distributed systems (e.g., database transactions, thread pools) and how TikTok-scale systems might handle deadlocks.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
SYN, SYN-ACK, ACK, then FIN/FIN-ACK for teardown.
Start by explaining the purpose of the three-way handshake and teardown in establishing and terminating reliable connections. Then walk through each step in sequence, using clear terminology and highlighting the flags (SYN, ACK, FIN) and state transitions. Finally, connect it to real-world implications like latency, resource usage, and trade-offs in system design.
Pro tip: Emphasize that the three-way handshake ensures both sides agree on initial sequence numbers and that teardown is a four-step process because TCP is full-duplex. Mention how this impacts performance (e.g., SYN flood attacks, TIME_WAIT) to show depth beyond textbook knowledge.
State that TCP is connection-oriented and requires a handshake to synchronize sequence numbers and establish parameters before data transfer.
Describe each step: SYN, SYN-ACK, ACK. Explain the flags, sequence number exchange, and state transitions (CLOSED, SYN_SENT, SYN_RECEIVED, ESTABLISHED).
Describe the four-step teardown: FIN, ACK, FIN, ACK. Highlight that each direction is closed independently and mention states like FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT.
Explain why the handshake and teardown are necessary (reliability, full-duplex) and their impact on performance (latency, resource consumption, TIME_WAIT).
Connect to real-world scenarios: how handshake latency affects user experience, how teardown states can cause port exhaustion, and potential optimizations (e.g., TCP Fast Open, connection pooling).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by defining the Java Memory Model (JMM) and its purpose: to provide a specification for how threads interact through memory, ensuring visibility and ordering. Then explain the happens-before relationship as the core guarantee, and detail how volatile and synchronized establish happens-before edges. Finally, contrast their mechanisms and use cases, emphasizing practical implications for concurrent programming.
Pro tip: Mention that volatile is sufficient for visibility and ordering but not atomicity, while synchronized provides both atomicity and visibility, and that happens-before is transitive—this shows depth and avoids common pitfalls.
Explain that the JMM specifies how threads see memory writes and that happens-before defines a partial order: if A happens-before B, then A's effects are visible to B and A is ordered before B.
Describe that a write to a volatile variable happens-before any subsequent read of that variable, ensuring visibility and preventing reordering of volatile accesses with other memory operations.
Describe that an unlock on a monitor happens-before a subsequent lock on the same monitor, ensuring visibility and providing mutual exclusion, which also gives atomicity for critical sections.
Highlight that volatile is lighter-weight but only guarantees visibility and ordering for single variables, while synchronized provides both visibility and atomicity for blocks, and can be used for compound actions.
Mention that happens-before is transitive, so combining volatile and synchronized can create safe publication, and that choosing between them depends on whether atomicity is needed and performance considerations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.