Professional Documents
Culture Documents
What Is Big O Notation and Why Software Engineers Must Know This Concept For Their Next Tech Interview by Tomasz Dobrowolski Medium
What Is Big O Notation and Why Software Engineers Must Know This Concept For Their Next Tech Interview by Tomasz Dobrowolski Medium
com
8–10 minutes
But what is Big O Notation? And why do you need to know about it?
John and Max are meeting for dinner on Friday evening, the busiest
time of the week for restaurants.
But how will they decide which one is the fastest option? and how
can they be sure the restaurant will not take too long during the
busiest time of the week?
• A server takes the customer’s order and a chef will prepare the
order on demand.
Restaurant 2 — Buffet:
• Once guests have paid, they can start serving themselves pre-
prepared food.
• The buffet contains multiple dishes and many chefs who continue
to refill the food.
• The restaurant is large with 30 tables.
John and Max can guess which restaurant will be the fastest option,
but they need to know for sure.
Restaurant 1
For John and Max to get their food, they rely on the server to seat
them, take their order, and for the chef to make their food.
There is only one chef cooking food and each meal takes 30
minutes to cook. Meaning for every customer in the restaurant, the
wait time for food grows by 30 minutes.
Restaurant 2
While the wait time to get food will increase as the restaurant gets
busier. It does not grow at a fixed rate like restaurant 1.
John and Max may have to wait a few minutes in a queue to get
their desired food, but that is as far as it goes.
Space Complexity
Meaning during the busiest time of the week for restaurant 1, the
guests will have to queue just to get inside.
John and Max can see that the buffet looks like the best restaurant
to eat food at.
1 being the most efficient process and 5 being the least efficient:
So let’s evaluate:
Now they decide to score the restaurants based on the least
efficient process for each complexity.
Time Complexity
Restaurant 1–5
Restaurant 2–2
Space Complexity
Restaurant 1–5
Restaurant 2–1
Restaurant 2 is the clear winner, with the lowest time and space
complexity.
John and Max can ease their mind knowing that restaurant 2 is the
best choice.
But what about when your website goes viral and you have 100,000
users on your system at once?
The same goes for a restaurant. Having one chef and a few tables
to serve customers is great when you start.
But once you have invested in a building and set up the restaurant,
it is a lot more costly to change things.
We will go into detail in a later article. Just know that the top (O(1))
is the most efficient, and the bottom (O(n!)) is the least efficient.
With Big O Notation we look at and assess every line of code in the
body of an algorithm.
Knowing this we can see the first line of code is linear, as it grows a
set amount as the input increases (we will cover linear growth in a
later article).
Looking inside the body of the for loop we can see two lines of
code.
Two echo statements which print the text ‘Hello’ and the number 4.
As this line of code does not change dependent on the input, we
can label this as a Constant growth. As there will be no change of
output regardless of the input size. Making it a hierarchy of O(1).
Out of the 3 lines of code with hierarchy labels. The least inefficient
part of the algorithm is the for loop on the first line with a Big O of
O(n).
Anyone can learn how to glue code together and make a program
work. But what separates great Software Engineers from good
Software Engineers, is the ability to detect and write efficient
algorithms.
Tune in for the next article, where we will teach you how to identify
each Big O Notation within the growth hierarchy.