nslookup and dig for DNS in Linux CLI - Time & Space Complexity
When using commands like nslookup and dig to query DNS, it helps to understand how the time to get results grows as we ask for more information.
We want to know how the command's work changes when we query more domains or request more details.
Analyze the time complexity of the following command sequence.
for domain in example.com example.org example.net; do
dig +short $domain
nslookup $domain
sleep 1
done
This script queries three domains using dig and nslookup, pausing briefly between each.
Look for repeated actions that take time.
- Primary operation: Running
digandnslookupcommands for each domain. - How many times: Once per domain, so 3 times in this example.
As the number of domains increases, the total time grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 queries each for dig and nslookup |
| 100 | 100 queries each for dig and nslookup |
| 1000 | 1000 queries each for dig and nslookup |
Pattern observation: The total work grows directly with the number of domains queried.
Time Complexity: O(n)
This means if you double the number of domains, the total time roughly doubles too.
[X] Wrong: "Running dig or nslookup once takes the same time no matter how many domains I query."
[OK] Correct: Each domain query is a separate operation that takes time, so more domains mean more total time.
Understanding how command execution time grows with input size helps you write scripts that scale well and avoid surprises when working with many domains.
What if we combined all domain queries into a single dig command? How would the time complexity change?