Difficulty: Beginner
What is the difference between a hub, a switch and a router? Explain collision and broadcast domains.
Networking hardware questions are favourites at Cisco and Juniper, but even service companies ask them. Let's build the answer in the order these devices historically evolved, because each one fixes a flaw of the previous one.
A hub is a layer 1 device. It has no intelligence: whatever electrical signal arrives on one port is repeated out of every other port. Imagine a meeting room where everyone shouts and everyone hears everything. Because all ports share one medium, if two devices transmit at once their signals collide. All ports of a hub therefore form a single collision domain, and the network used CSMA/CD to recover from collisions. Bandwidth is shared, half-duplex, and anyone can sniff everyone's traffic. Hubs are essentially obsolete now.
A switch is a layer 2 device. It reads the destination MAC address of each frame and forwards it only to the port where that MAC lives. It learns this dynamically: when a frame arrives, the switch records the source MAC against the incoming port in its MAC address table (also called the CAM table). If the destination is unknown, or it is a broadcast, the switch floods the frame out of all ports except the incoming one. Each switch port is its own collision domain, and with full-duplex links collisions basically disappear. But all ports of a switch, unless you configure VLANs, are still in one broadcast domain: a broadcast like an ARP request reaches every device.
A router is a layer 3 device. It connects different networks and forwards packets based on destination IP address using a routing table. Each router interface is its own broadcast domain, so routers do not forward broadcasts. That is how we contain broadcast storms and segment large networks. Routers also do things like NAT, ACL-based filtering, and running routing protocols such as OSPF and BGP.
To summarise the domains: a hub has one collision domain and one broadcast domain. A switch has one collision domain per port but one broadcast domain (per VLAN). A router has one collision domain and one broadcast domain per interface. If an interviewer gives you a diagram with 1 router, 2 switches and 10 PCs and asks you to count, count collision domains as the number of switch ports in use plus router interfaces, and broadcast domains as the number of router interfaces (or VLANs).
Edge cases to mention: a layer 3 switch combines both, doing fast hardware switching within a VLAN and routing between VLANs. VLANs let one physical switch behave like several logically separate switches, and inter-VLAN traffic must go through a router. Also, a bridge is basically an older two-port switch. When asked which device you would use to connect your home network to the internet, the answer is a router (your home Wi-Fi box is actually a router plus switch plus access point plus NAT plus DHCP server in one plastic case).
PC-A (MAC AA) on port 1 sends frame to PC-B (MAC BB)
Step 1: switch learns AA -> port 1
Step 2: BB unknown -> flood to ports 2,3,4
Step 3: PC-B replies from port 3 -> switch learns BB -> port 3
Step 4: future A<->B frames go only to ports 1 and 3
Flooding happens only until the table is populated. Entries age out after a timeout, typically 300 seconds.
Hub, Switch, Router, Collision Domain, Broadcast Domain