Bird
Raised Fist0
Interview Prepcomputer-networksmediumAmazonGoogleMicrosoftFlipkartSwiggy

NAT - Network Address Translation, Types & Port Forwarding

Choose your preparation mode3 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
🎯
NAT - Network Address Translation, Types & Port Forwarding
mediumNETWORKSAmazonGoogleMicrosoft

Imagine your home network with multiple devices sharing a single public IP address to access the internet seamlessly without conflicts.

💡 Many beginners confuse NAT with just IP masking or think NAT only hides IP addresses, missing its role in routing and port management.
📋
Interview Question

Explain what Network Address Translation (NAT) is, describe its main types (SNAT, DNAT, PAT), and explain how port forwarding works.

Purpose and function of NAT in IPv4 networksDifferences between Source NAT (SNAT), Destination NAT (DNAT), and Port Address Translation (PAT)How port forwarding enables external access to internal network services
💡
Scenario & Trace
ScenarioA home router allows multiple devices to access the internet using one public IP address.
Each device has a private IP; when a device sends a request, the router replaces the private source IP with its public IP and assigns a unique source port (PAT). When the response returns, the router uses the port mapping to forward the packet back to the correct internal device.
ScenarioA company wants to allow external users to access an internal web server hosted on a private IP.
The router uses DNAT to translate incoming packets destined for the public IP and a specific port to the private IP and port of the internal web server, enabling external access without exposing the internal IP.
  • What happens if two internal devices try to use the same source port simultaneously in PAT?
  • How does NAT handle protocols that embed IP addresses in the payload (e.g., FTP)?
  • What occurs when port forwarding is set up for a service but the internal server is offline?
⚠️
Common Mistakes
Thinking NAT only hides IP addresses without changing ports

Interviewer doubts your understanding of PAT and session multiplexing

Explain that PAT changes both IP and port to allow multiple sessions

Confusing SNAT and DNAT as the same thing

Interviewer sees lack of clarity on direction and purpose of translation

Clarify SNAT modifies source IP for outgoing traffic; DNAT modifies destination IP for incoming traffic

Assuming port forwarding exposes internal IPs directly

Interviewer questions your grasp of security implications

Explain port forwarding translates public IP and port to private IP and port without exposing internal IP externally

Ignoring how NAT handles protocols with embedded IP addresses

Interviewer suspects you lack practical knowledge of NAT limitations

Mention protocols like FTP require special handling (ALG) because NAT can't rewrite payload IPs

🧠
Basic Definition - What It Is
💡 This is the minimum you must know to explain NAT in any interview.

Intuition

NAT translates private IP addresses to a public IP address so multiple devices can share one internet connection.

Explanation

Network Address Translation (NAT) is a technique used in IPv4 networks to map multiple private IP addresses within a local network to a single public IP address for communication over the internet. It helps conserve public IP addresses and adds a layer of security by hiding internal IPs. The most common form is when a home router translates all internal device IPs to its single public IP.

Memory Hook

💡 Think of NAT as a receptionist who forwards calls from one public phone number to multiple internal extensions.

Interview Questions

What is the primary purpose of NAT?
  • Conserves public IPv4 addresses by sharing one IP among many devices
  • Hides internal network structure for security
Depth Level
Interview Time30 seconds
Depthbasic

Covers the fundamental concept and purpose of NAT, sufficient for screening rounds.

Interview Target: Minimum floor - never go below this

Knowing only this will help you pass initial screening but not detailed technical rounds.

🧠
Mechanism Depth - How It Works
💡 This is what product companies expect you to understand and explain clearly.

Intuition

NAT modifies IP addresses and ports in packet headers to enable multiple devices to communicate externally using a single public IP.

Explanation

NAT operates by rewriting the source or destination IP addresses and sometimes ports in IP packet headers as they pass through a router or firewall. Source NAT (SNAT) changes the source IP of outgoing packets from private to public IP, enabling devices to access the internet. Destination NAT (DNAT) modifies the destination IP of incoming packets to route them to the correct internal device, often used in port forwarding. Port Address Translation (PAT), a form of SNAT, also changes source ports to uniquely identify sessions, allowing many devices to share one public IP simultaneously. NAT maintains a translation table to track these mappings and correctly route return traffic. Port forwarding is a DNAT application where specific external ports are mapped to internal IPs and ports, enabling external access to services like web servers inside a private network.

Memory Hook

💡 Imagine a hotel concierge who not only forwards calls but also assigns unique room numbers (ports) to guests sharing the same phone line (IP).

Interview Questions

How does PAT allow multiple devices to share one public IP?
  • By translating both source IP and source port of outgoing packets
  • Assigns unique source ports to distinguish sessions
  • Maintains a NAT table to map ports back to internal IPs
Explain how port forwarding works using DNAT.
  • Incoming packets to a public IP and port are translated to a private IP and port
  • Allows external users to access internal services securely
  • Requires static mapping in the NAT device
Depth Level
Interview Time2-3 minutes
Depthintermediate

Demonstrates understanding of NAT internals, types, and practical applications like port forwarding.

Interview Target: Target level for FAANG on-sites

Mastering this level distinguishes you from most candidates and prepares you for detailed technical discussions.

📊
Explanation Depth Levels
💡 Choose your explanation depth based on interview stage and company expectations.
LevelInterview TimeSuitable ForRisk
Basic Definition30sScreening call or initial roundsToo shallow for on-site or deep technical rounds
Mechanism Depth2-3 minutesOn-site interviews at FAANG and top tech companiesRequires good understanding; missing details may lose points
💼
Interview Strategy
💡 Use this guide to structure your explanation and anticipate common questions during mock interviews.

How to Present

Start with a clear definition of NAT and its purposeGive a relatable example or analogy (e.g., home router sharing one IP)Explain the different types of NAT: SNAT, DNAT, and PATDescribe how port forwarding works and why it is usefulMention common edge cases or challenges with NAT

Time Allocation

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

What the Interviewer Tests

Your ability to clearly explain NAT's purpose, mechanisms, and practical use cases, plus handling of tricky scenarios like port conflicts or protocol issues.

Common Follow-ups

  • What happens if two devices try to use the same source port in PAT? → The NAT device assigns different external ports to avoid conflict.
  • How does NAT affect protocols that embed IP addresses in payloads? → Special handling or application-level gateways are needed.
💡 These follow-ups test your deeper understanding and ability to handle real-world NAT challenges.
🔍
Pattern Recognition

When to Use

When asked about IP address management, internet access for private networks, or firewall/router configurations.

Signature Phrases

'Explain Network Address Translation''Compare SNAT vs DNAT''What happens when multiple devices share one public IP?'

NOT This Pattern When

Similar Problems

Practice

(1/5)
1. Trace the sequence of layers a data packet passes through when a user sends an email using SMTP over TCP/IP. Which order correctly represents the encapsulation process from the sender's perspective?
easy
A. Application -> Transport -> Internet -> Network Interface
B. Network Interface -> Internet -> Transport -> Application
C. Internet -> Transport -> Application -> Network Interface
D. Transport -> Application -> Internet -> Network Interface

Solution

  1. Step 1: Understand encapsulation order in TCP/IP model

    Data starts at the Application Layer (SMTP), then is passed down to Transport Layer (TCP) for segmentation and connection management, then to Internet Layer (IP) for routing, and finally to Network Interface Layer for physical transmission.
  2. Final Answer:

    Option A -> Option A
  3. Quick Check:

    Encapsulation order is top-down: Application -> Transport -> Internet -> Network Interface [OK]
Hint: Encapsulation flows top-down from Application to Network Interface
Common Mistakes:
  • Confusing encapsulation with decapsulation order
  • Mixing up Internet and Transport layers
  • Assuming Network Interface is the first layer
2. Trace the sequence of packets exchanged during the TCP three-way handshake when a client initiates a connection to a server.
easy
A. Client sends ACK, Server replies with SYN, Client sends SYN-ACK
B. Client sends SYN-ACK, Server replies with ACK, Client sends SYN
C. Client sends SYN, Server replies with SYN-ACK, Client sends ACK
D. Client sends SYN, Server replies with ACK, Client sends SYN-ACK

Solution

  1. Step 1: Understand the handshake steps

    The client initiates by sending a SYN packet to request connection. The server responds with SYN-ACK to acknowledge and synchronize. The client completes with ACK.
  2. Step 2: Evaluate each option

    Client sends SYN, Server replies with SYN-ACK, Client sends ACK correctly follows the SYN -> SYN-ACK -> ACK sequence. Options A, C, and D have the order or packet types mixed up, which breaks the handshake protocol.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Remember the handshake as "SYN, SYN-ACK, ACK" in that order.
Hint: Handshake order: SYN -> SYN-ACK -> ACK
Common Mistakes:
  • Mixing up who sends SYN-ACK or ACK first
  • Assuming ACK comes before SYN-ACK
3. Which of the following statements about DHCP IP address leasing is INCORRECT?
medium
A. Once a lease expires, the IP address is immediately available for reassignment
B. Clients must renew their lease before it expires to keep the same IP address
C. DHCP servers can reclaim IP addresses from clients that do not renew leases
D. Leases help prevent IP address conflicts in the network

Solution

  1. Step 1: Understand lease expiration behavior

    When a lease expires, the IP address is not immediately available; there is usually a grace period before reassignment to avoid conflicts.
  2. Step 2: Analyze other options

    Options B, C, and D are correct statements about DHCP leasing behavior.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Immediate reassignment after expiration is incorrect; DHCP uses timers to prevent conflicts.
Hint: Lease expiration ≠ instant IP reuse; timers prevent conflicts
Common Mistakes:
  • Assuming IPs are instantly reusable after lease expiry
  • Confusing lease renewal with release
  • Believing leases do not prevent conflicts
4. Which of the following best explains why TCP's sliding window flow control alone cannot prevent network congestion?
medium
A. Because sliding window increases window size exponentially regardless of network state
B. Because sliding window only limits sender based on receiver buffer, not network capacity
C. Because sliding window retransmits lost packets too slowly
D. Because sliding window requires explicit congestion notification from routers

Solution

  1. Step 1: Understand sliding window's limitation

    Sliding window controls flow based on receiver's buffer availability, not network congestion.
  2. Step 2: Differentiate from congestion control

    Congestion control algorithms like AIMD adjust sending rate based on network feedback to avoid congestion.
  3. Step 3: Reject exponential increase and retransmission speed options

    Sliding window does not inherently increase window exponentially; retransmission speed is unrelated to flow control.
  4. Step 4: Clarify explicit congestion notification role

    Explicit congestion notification is optional and unrelated to sliding window's basic operation.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    Sliding window limits sender by receiver buffer, not network capacity.
Hint: Flow control = receiver buffer limit; congestion control = network capacity limit.
Common Mistakes:
  • Assuming sliding window adapts to network congestion
  • Confusing flow control with retransmission mechanisms
  • Believing sliding window depends on router signals
5. Why might the TCP three-way handshake introduce latency, and when could this be a disadvantage?
medium
A. Because it requires multiple round-trip times before data transfer, causing delay in time-sensitive applications
B. Because it encrypts all packets during handshake, increasing processing time
C. Because it uses UDP packets which are slower than TCP packets
D. Because it requires the server to send data before the client can send any

Solution

  1. Step 1: Identify handshake latency cause

    The handshake requires at least one full round-trip time (RTT) before data can be sent, introducing delay.
  2. Step 2: Analyze options

    Because it requires multiple round-trip times before data transfer, causing delay in time-sensitive applications correctly states the latency due to multiple RTTs, which can be problematic for real-time or low-latency applications. Because it encrypts all packets during handshake, increasing processing time is incorrect; encryption is separate from handshake. Because it uses UDP packets which are slower than TCP packets is false; handshake uses TCP packets, not UDP. Because it requires the server to send data before the client can send any is incorrect; the client sends data after handshake completes.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Handshake latency = multiple RTTs before data flow.
Hint: Handshake latency = waiting for multiple packet exchanges before data
Common Mistakes:
  • Confusing handshake with encryption overhead
  • Thinking handshake uses UDP packets
  • Believing server sends data first