Bird
Raised Fist0
Interview Prepcomputer-networkseasyAmazonGoogleMicrosoftFlipkartSwiggyRazorpay

TCP Three-Way Handshake - SYN, SYN-ACK, ACK & Connection Teardown

Choose your preparation mode3 modes available
🎯
TCP Three-Way Handshake - SYN, SYN-ACK, ACK & Connection Teardown
easyNETWORKSAmazonGoogleMicrosoft

Imagine you want to call a friend but need to make sure they are ready to talk before you start the conversation. TCP’s three-way handshake is like that initial call setup ensuring both sides are ready.

💡 Beginners often confuse the handshake as just sending SYN packets or think connection teardown is symmetrical like setup, missing the four-step process and the purpose of each packet. Think of the handshake as a polite phone call setup, and teardown as hanging up carefully so both parties know the call ended.
📋
Interview Question

Explain the TCP three-way handshake process including SYN, SYN-ACK, and ACK packets, and describe how TCP connection teardown works.

Purpose of TCP three-way handshake for reliable connection establishmentRoles of SYN, SYN-ACK, and ACK packets in handshakeFour-step TCP connection termination process
💡
Scenario & Trace
ScenarioA client wants to establish a TCP connection with a web server to load a webpage.
1. Client sends SYN to server to request connection. 2. Server responds with SYN-ACK to acknowledge and synchronize. 3. Client sends ACK to confirm connection established. 4. Data transfer begins. 5. When done, client sends FIN to initiate teardown. 6. Server replies with ACK, then sends its own FIN. 7. Client sends final ACK, connection closes.
  • What if SYN packet is lost during handshake → retransmission and timeout handling
  • What if one side crashes after sending SYN-ACK but before receiving ACK → half-open connection scenario
  • What if FIN packet is lost during connection teardown → retransmission and connection timeout
⚠️
Common Mistakes
Thinking the handshake is just a SYN and SYN-ACK exchange

Interviewer doubts your understanding of the ACK’s role in confirming connection establishment

✅ Explain the three steps clearly and emphasize the final ACK from client confirms the connection

Assuming connection teardown is symmetrical and uses three steps like setup

Interviewer suspects you don’t know about the four-step FIN/ACK exchange and half-close states

✅ Describe the four-way handshake and explain why both sides must close independently

Ignoring the role of sequence numbers in handshake synchronization

Interviewer thinks you lack understanding of TCP reliability and ordering

✅ Mention initial sequence numbers and how acknowledgments confirm synchronization

Not considering packet loss and retransmission during handshake and teardown

Interviewer doubts your knowledge of TCP’s robustness and timeout mechanisms

✅ Explain retransmission timers and how TCP handles lost SYN, ACK, or FIN packets

🧠
Basic Definition - What It Is
💡 This level covers the fundamental purpose and sequence of the handshake and teardown without deep internals. It’s like knowing the main steps of a phone call setup and hang-up without the technical details.

Intuition

TCP uses a three-step message exchange to establish a reliable connection before data transfer.

Explanation

TCP is a connection-oriented protocol that requires both client and server to agree on connection parameters before exchanging data. The three-way handshake involves the client sending a SYN packet to initiate, the server replying with SYN-ACK to acknowledge and synchronize, and the client sending an ACK to confirm. This ensures both sides are ready and synchronized. Connection teardown is done via a four-step process where FIN and ACK packets are exchanged to gracefully close the connection.

Memory Hook

💡 Think of it as a polite phone call: 'Hello?' (SYN), 'Hello, I hear you!' (SYN-ACK), 'Great, let's talk!' (ACK), and hanging up politely with goodbye signals (FIN/ACK).

Illustrative Code

# No code needed for basic definition approach
# This approach focuses on conceptual understanding
pass

Interview Questions

❓ What are the three packets involved in TCP connection setup?
  • SYN from client
  • SYN-ACK from server
  • ACK from client
⏱
Depth Level
Interview Time30 seconds
Depthbasic

Covers the basic handshake steps and purpose; sufficient for quick screening questions. No algorithmic complexity applies as this is conceptual.

Interview Target: Minimum floor - never go below this

Knowing only this lets you pass initial screening but won't impress in detailed interviews.

🧠
Mechanism Depth - How It Works
💡 This level explains the internal mechanisms, packet flags, sequence numbers, and connection teardown details expected in product company interviews. Think of it as understanding the secret handshake and the exact steps to close the call cleanly.

Intuition

TCP handshake synchronizes sequence numbers and confirms readiness, while teardown ensures both sides close connection gracefully to avoid resource leaks.

Explanation

The three-way handshake starts with the client sending a SYN packet with an initial sequence number (ISN). The server responds with a SYN-ACK packet containing its own ISN and acknowledging the client's ISN+1. The client then sends an ACK acknowledging the server's ISN+1. This exchange synchronizes sequence numbers and confirms both sides are ready. For connection teardown, TCP uses a four-way handshake: one side sends FIN to indicate no more data, the other side acknowledges with ACK, then sends its own FIN, and finally the first side sends an ACK. This ensures both sides have finished sending data and the connection closes cleanly. Retransmissions handle lost packets, and TCP timers manage connection states.

Memory Hook

💡 Imagine two people exchanging secret handshake steps to start a dance (synchronize), then carefully stepping off the floor one by one to avoid collisions (teardown).

Illustrative Code

# No code needed for mechanism depth approach
# This approach focuses on detailed conceptual understanding
pass

Interview Questions

❓ What role do sequence numbers play in the TCP handshake?
  • Initial sequence numbers synchronize sender and receiver
  • Acknowledgments confirm receipt
  • Prevents old duplicate packets from causing confusion
❓ Explain the four steps in TCP connection teardown.
  • FIN from initiator
  • ACK from receiver
  • FIN from receiver
  • ACK from initiator
⏱
Depth Level
Interview Time2-3 minutes
Depthintermediate

Demonstrates understanding of TCP internals, sequence numbers, flags, and teardown process; suitable for FAANG on-sites. No algorithmic complexity applies as this is conceptual.

Interview Target: Target level for FAANG on-sites

Mastering this level distinguishes you from most candidates and shows readiness for system design and networking roles.

📊
Explanation Depth Levels
💡 Choose your explanation depth based on interview stage and company expectations.
LevelInterview TimeSuitable ForRisk
Basic Definition30sScreening call or quick conceptual questionsToo shallow for on-site or system design interviews
Mechanism Depth2-3 minutesOn-site interviews at FAANG and top tech companiesRequires solid understanding; missing details here can cost you
💼
Interview Strategy
💡 Use this guide to structure your explanation clearly and confidently before every networking mock interview.

How to Present

Start with a concise definition of the TCP three-way handshake and its purpose.Give a relatable analogy or example to make it memorable.Explain the detailed mechanism including packet types and sequence numbers.Describe the connection teardown process and mention common edge cases.

Time Allocation

Definition: 30s → Example: 1min → Mechanism: 2min → Edge cases: 30s. Total ~4min

What the Interviewer Tests

Interviewer checks if you understand why handshake is needed, how synchronization happens, and how TCP ensures reliable connection setup and teardown.

Common Follow-ups

  • What happens if a SYN packet is lost? → Retransmission and timeout mechanisms.
  • Why is the connection teardown a four-way handshake instead of three? → Half-close states and independent closing.
💡 These follow-ups test your grasp on TCP reliability and connection state management.
🔍
Pattern Recognition

When to Use

Interviewers ask about this when discussing TCP, connection establishment, or reliable communication protocols.

Signature Phrases

Explain the TCP three-way handshakeWhat happens when a TCP connection is established?Describe TCP connection teardown

NOT This Pattern When

Similar Problems

Practice

(1/5)
1. When a browser needs to resolve a domain name to an IP address, which DNS server is responsible for directing the query to the appropriate Top-Level Domain (TLD) server?
easy
A. The local DNS resolver on the user's machine
B. The root DNS server
C. The authoritative DNS server for the domain
D. The recursive DNS server operated by the ISP

Solution

  1. Step 1: Understand DNS hierarchy

    The DNS system is hierarchical: root servers are at the top, followed by TLD servers, then authoritative servers.
  2. Step 2: Role of root DNS servers

    Root servers do not resolve domain names directly but direct queries to the correct TLD server based on the domain extension (e.g., .com, .org).
  3. Step 3: Why other options are incorrect

    Local resolvers and recursive servers initiate queries but do not direct to TLD servers; authoritative servers provide final answers for domains.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Root DNS servers direct queries to TLD servers -> correct
Hint: Root servers direct queries to TLD servers, not resolve domains themselves.
Common Mistakes:
  • Confusing recursive DNS servers with root servers
  • Assuming local resolver directs to TLD servers
  • Thinking authoritative servers handle query routing
2. Trace the sequence of events when an external client accesses an internal web server via port forwarding configured on a NAT router.
easy
A. The router changes the destination IP and port to the internal server's IP and port, forwards the packet, and rewrites the source IP of the reply back to the router's public IP.
B. The router changes the destination IP and port to the internal server's IP and port, then forwards the packet; the server replies directly to the client.
C. The router only changes the destination IP but leaves the port unchanged; the internal server receives the packet and replies to the router.
D. The router forwards the packet without any translation; the internal server replies directly to the external client.

Solution

  1. Step 1: Understand port forwarding (DNAT)

    Port forwarding rewrites destination IP and port of incoming packets to internal server's IP and port.
  2. Step 2: Trace reply path

    The server replies to the router's internal IP, which rewrites the source IP back to its public IP before sending to the external client.
  3. Step 3: Analyze options

    The router changes the destination IP and port to the internal server's IP and port, forwards the packet, and rewrites the source IP of the reply back to the router's public IP correctly describes both forward and reply translation. The router changes the destination IP and port to the internal server's IP and port, then forwards the packet; the server replies directly to the client is incorrect because the server cannot reply directly to the client without NAT rewriting. The router only changes the destination IP but leaves the port unchanged; the internal server receives the packet and replies to the router misses port translation. The router forwards the packet without any translation; the internal server replies directly to the external client ignores NAT translation entirely.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Port forwarding requires bidirectional NAT translation [OK]
Hint: Port forwarding rewrites destination IP/port inbound and source IP outbound
Common Mistakes:
  • Assuming server replies directly to external client
  • Forgetting port translation in forwarding
  • Ignoring reply path NAT rewriting
3. Trace the encapsulation process when a user sends an email from their client application. Which sequence correctly represents the PDUs as data moves down the OSI layers?
easy
A. Data -> Packet -> Segment -> Frame -> Bits
B. Bits -> Frame -> Packet -> Segment -> Data
C. Data -> Segment -> Packet -> Frame -> Bits
D. Segment -> Data -> Packet -> Frame -> Bits

Solution

  1. Step 1: Identify PDU names per OSI layer

    Application Layer and Presentation Layer use 'Data', Transport Layer uses 'Segment', Network Layer uses 'Packet', Data Link Layer uses 'Frame', Physical Layer uses 'Bits'.
  2. Step 2: Follow encapsulation order

    Data is segmented, then packets are created, frames are formed, and finally bits are transmitted.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Correct encapsulation order matches OSI layering [OK]
Hint: Data -> Segment -> Packet -> Frame -> Bits follows OSI top-down encapsulation [OK]
Common Mistakes:
  • Confusing order of PDUs
  • Mixing up Segment and Packet roles
  • Reversing encapsulation direction
4. Which of the following is a limitation of using a proxy server for improving network security compared to a firewall?
medium
A. Proxy servers cannot filter traffic based on IP addresses and ports
B. Proxy servers cannot cache content to improve performance
C. Proxy servers introduce significant latency due to encryption overhead
D. Proxy servers block all incoming traffic by default

Solution

  1. Step 1: Proxy server capabilities

    Proxy servers act as intermediaries forwarding client requests but typically do not filter traffic at the IP/port level.
  2. Step 2: Firewall capabilities

    Firewalls filter traffic based on IP addresses, ports, and protocols, providing network-level security.
  3. Step 3: Evaluate other options

    Proxy servers can cache content (contradicts C), do not block all incoming traffic by default (contradicts D), but may introduce latency due to encryption overhead in some cases.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Proxy servers may introduce latency due to encryption overhead, unlike firewalls [OK]
Hint: Proxy forwards requests; firewall filters traffic.
Common Mistakes:
  • Assuming proxies filter traffic like firewalls
  • Confusing caching capabilities of proxies
5. What is a common misconception about the performance impact of HTTPS compared to HTTP?
medium
A. HTTPS can be faster than HTTP if HTTP/2 is used because HTTP/2 requires TLS
B. The TLS handshake overhead is amortized over the session, so HTTPS impact is minimal for long connections
C. HTTPS always doubles the latency because of encryption overhead
D. HTTPS requires more CPU but modern hardware and session resumption reduce impact significantly

Solution

  1. Step 1: Understand HTTPS performance characteristics

    Encryption adds overhead, but it is not always a fixed doubling of latency.
  2. Step 2: Analyze options

    A: Correct that HTTP/2 requires TLS and can improve performance.
    B: Correct understanding that handshake cost is amortized over session.
    C: Correct that hardware and session resumption reduce CPU impact.
    D: Incorrect misconception that HTTPS always doubles latency.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    HTTPS overhead is often overestimated; it depends on connection length and optimizations.
Hint: HTTPS overhead is upfront, then minimal per request
Common Mistakes:
  • Assuming HTTPS always causes large latency increase
  • Ignoring session resumption benefits
  • Believing HTTP/2 can be used without TLS
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard