Sunteți pe pagina 1din 9

Big O Notation Explained with an Example

1 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

Translate:

Home
Java
C/C++
Databases/SQL
Javascript
PHP

How does Big-O Notation work, and can you provide an


example?

Data Structures
Data Structure Interview Questions
and Answers
Big O Notation

First and foremost, do not even walk into a software interview without knowing what Big
O Analysis is all about you will embarrass yourself. Big O Notation is simply something
that you must know if you expect to get a job in this industry. Here we present a tutorial

Big O versus Big Omega notations

on Big O Notation, along with some simple examples to really help you understand it. You

DFS vs. BFS

can consider this article to be sort of a big O notation for dummies tutorial, because we
really try to make it easy to understand.

Hash Tables versus Binary Search


Trees

What is Big O Analysis in computer science a tutorial:

How to find if a linked list is circular


has a cycle or ends

When solving a computer science

Preorder Traversal Algorithm

problem there will usually be more


than just one solution. These

Inorder Traversal

solutions will often be in the form of

Postorder Traversal

different algorithms, and you will


generally want to compare the

Difference between stack and heap

algorithms to see which one is more

Find nth to last element in a linked list

efficient.

Delete a node in the middle of a singly


linked list

This is where Big O analysis helps

Reverse a linked list

the efficiency of an algorithm. A

it gives us some basis for measuring


more detailed explanation and

Design Pattern Questions

definition of Big O analysis would be


this: it measures the efficiency of an

Excel Interview Questions


HTML5

algorithm based on the time it takes


for the algorithm to run as a function
of the input size. Think of the input simply as what goes into a function whether it be
an array of numbers, a linked list, etc.

Networking

Sounds quite boring, right?


Operating Systems

Its really not that bad at all and it is something best illustrated by an example with
Recursion

actual code samples.

Apache Interview Questions

Big O Notation Practice Problems

General/Miscellaneous
Non-Technical Questions
Interviewing in India
Working As a Software Engineer

Even if you already know what Big O Notation is, you can still check out the example
algorithms below and try to figure out the Big O Notation of each algorithm on your own
without reading our answers first. This will give you some good practice finding the Big O
Notation on your own using the problems below.

Big O Notation Examples in Java


Now its really time to pay attention
lets start our explanation of Big O

Financial Analyst Questions


Job Advice For Programmers
Puzzles

Notation with an actual problem.


Here is the problem we are trying to
solve:
Lets suppose that we want to
create a function that, when

Assortment of Knowledge

given an array of integers


greater than 0, will return the
integer that is the smallest in

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

that array.

American Vocabulary

In order to best illustrate the way


Technical Vocabulary

Big-O analysis works, we will come


up with two different solutions to

Science Questions

this problem, each with a different


Big-O efficiency.
Heres our first function that will
simply return the integer that is the
smallest in the array. The algorithm
will just iterate through all of the
values in the array and keep track of

Email
Country

the smallest integer in the array in

United States

the variable called curMin.


Lets assume that the array being
passed to our function contains 10 elements this number is something we arbitrarily
chose. We could have said it contains 100, or 100000 elements either way it would
have made no difference for our purposes here.

The CompareSmallestNumber Java function

Secure Connection
Failed

2 of 9

int CompareSmallestNumber (int array[ ])


{
int x, curMin;
// set smallest value to first item in array
curMin = array[0];
/* iterate through array to find smallest value
and also assume there are only 10 elements
*/
for (x = 1; x < 10; x++)
{
if( array[x] < curMin) {
curMin = array[x];
}
}
// return smallest value in the array
return curMin;
}

As promised, we want to show you another solution to the problem. In this solution, we
will use a different algorithm - we will soon compare the big O Notation of the two
different solutions below. What we do for our second solution to the problem is compare
each value in the array to all of the other numbers in the array, and if that value is less
than or equal to all of the other numbers in the array then we know that it is the smallest
number in the array.

The CompareToAllNumbers Java function


int CompareToAllNumbers (int array[ ])
{
bool is Min;
int x, y;
/* iterate through each element in array,
assuming there are only 10 elements:
*/
for (int x = 0; x < 10; x++)
{
isMin = true;

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

3 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

for (int y = 0; y < 10; y++)


{
/* compare the value in array[x] to the other values
if we find that array[x] is greater than any of the
values in array[y] then we know that the value in
array[x] is not the minimum
remember that the 2 arrays are exactly the same, we
are just taking out one value with index 'x' and
comparing to the other values in the array with
index 'y'
*/
if( array[x] > array[y])
isMin = false;
}
if(isMin)
break;
}
return array[x];
}

Now, you've seen 2 functions that solve the same problem - but each one uses a different
algorithm. We want to be able to say which algorithm is more efficient using
mathematical terms, and Big-O analysis allows us to do exactly that.

Big O analysis of algorithms


For our purposes, we assumed an input size of 10 for the array. But when doing Big O
analysis, we don't want to use specific numbers for the input size - so we say that the
input is of size n.
Remember that Big-O analysis is used to measure the efficiency of an algorithm based on
the time it takes for the algorithm to run as a function of the input size.
When doing Big-O analysis, "input" can mean a lot of different things depending on the
problem being solved. In our examples above, the input is the array that is passed into
the different functions. But, input could also be the number of elements of a linked list,
the nodes in a tree, or whatever data structure you are dealing with.
Since input is of size n, and in our example the input is an array - we will say that the
array is of size n. We will use the 'n' to denote input size in our Big-O analysis.

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

4 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

So, the real question is how Big-O analysis measures efficiency. Basically, Big-O will want
to express how many times the 'n' input items are 'touched'. The word 'touched' can
mean different things in different algorithms - in some algorithms it may mean the
number of times a constant is multiplied by an input item, the number of times an input
is added to a data structure, etc.
But in our functions CompareSmallestNumber and CompareToAllNumbers, it just means
the number of times an array value is compared to another value.

Big O notation time complexity


In the function CompareSmallestNumber, the n (we used 10 items, but lets just use the
variable 'n' for now) input items are each 'touched' only once when each one is compared
to the minimum value. In Big O notation, this would be written as O(n) - which is also
known as linear time. Linear time means that the time taken to run the algorithm
increases in direct proportion to the number of input items. So, 80 items would take
longer to run than 79 items or any quantity less than 79. Another way to phrase this is to
say that the algorithm being used in the CompareSmallestNumber function has order of n
time complexity.
Subscribe to our newsletter for more free interview questions.
You might also see that in the CompareSmallestNumber function, we initialize the curMin
variable to the first value of the input array. And that does count as 1 'touch' of the input.
So, you might think that our Big O notation should be O(n + 1). But actually, Big O is
concerned with the running time as the number of inputs - which is 'n' in this case approaches infinity. And as 'n' approaches infinity the constant '1' becomes very
insignificant - so we actually drop the constant. Thus, we can say that the
CompareSmallestNumber function has O(n) and not O(n + 1).
Also, if we have n3 + n, then as n approaches infinity it's clear that the "+ n" becomes
very insignificant - so we will drop the "+ n", and instead of having O(n3 + n), we will
have O(n3 ), or order of n3 time complexity.

What is Big O notation worst case?


Now, let's do the Big O analysis of the CompareToAllNumbers function. The worst case
of Big O notation in our example basically means that we want to find the scenario which
will take the longest for the CompareToAllNumbers function to run. When does that
scenario occur?

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

5 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

Well, let's think about what the worst case running time for the CompareToAllNumbers
function is and use that as the basis for the Big O notation. So, for this function, let's
assume that the smallest integer is in the very last element of the array - because that is
the exact scenario which will take the longest to run since it will have to get to the very
last element to find the smallest element. Since we are taking each element in the array
and comparing it to every other element in the array, that means we will be doing 100
comparisons - assuming, of course, that our input size is 10 (10 * 10 = 100). Or, if we
use a variable "n" to represent the input size, that will be n2 'touches' of the input. Thus,
this function uses a O(n2 ) algorithm.

Big O analysis measures efficiency


Now, let's compare the 2 functions: CompareToAllNumbers is O(n2 ) and
CompareSmallestNumber is O(n). So, let's say that we have 10,000 input elements, then
CompareSmallestNumber will 'touch' on the order of 10,000 elements, whereas
CompareToAllNumbers will 'touch' 10,000 squared or 100,000,000 elements. That's a
huge difference, and you can imagine how much faster CompareSmallestNumber must
run when compared to CompareToAllNumbers - especially when given a very large
number of inputs. Efficiency is something that can make a huge difference and it's
important to be aware of how to create efficient solutions.
In an interview, you may be asked what the Big-O of an algorithm that you've come up
with is. And even if not directly asked, you should provide that information in order to
show that you are well aware of the need to come up with an efficient solution whenever
possible.

What about Big Omega notation?


Big O and Big Omega notations are not the same thing. You can read about the
differences here: Big O versus Big Omega.

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

6 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

Hiring? Job Hun ng? Post a JOB or your RESUME on our JOB BOARD >>
Follow @programmerintvw

FOLLOW Varoon Sahgal, Author of ProgrammerInterview on

Previous...

Next...

Muaz Aag
Justify
why the complexity of all stack methods are O(1)?. Your justification MUST be
supported by reliable sources. help me.. email at muazaag@gmail.com
MoeMoe
Poor joejoe, if only you could focus and apply as much effort to learning as you do
trolling you may be able to understand things such as these
joejoe
tis is the worst tut0rial evahh
I have a lot of IQ so im not stupeed .
stop thanking this post you Poser NErds .
pepe
Fuck you.
joejoe
wanna piece of tiss ?
joejoe
those who that this is helpful . thats just BULLSHIT ..
Mayank
Awesome explanation!!
joejoe
Bullshit .
Zach Peterson
Remember that Big-O analysis is used to measure the efficiency of an algorithm
based on the time it takes for the algorithm to run as a function of the input size.

FYI it doesnt use TIME, its STEPS. Big O does -not- measure time in any way,
fashion or form. Why? Because processors, compiler optimization algorithms, etc.,
all have isms that can improve a functions time over what you can describe
mathematically.

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

7 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

So, its based on STEPS. Steps = complexity.


Big O does not compare to Function Points. The IFPUG (.org) has been doing this
for decades longer than Big O has been a buzz ward, and is far more reliable
imho. With complexity measurements using function points, I can tell you not
only how complex a function is but how long itll take to implement and how much
itll cost you to do so.
Dr. B
alka
for(icount2=1;icount<=n;icount2=icount2*2)
{
for(icount3=1;icount3<=n;icount3=icount3*2)
{

..}
}
}
what is the time complexity of this code????????
please do reply
thnku
Anand Singh
Nice explanation. Good
joejoe
poser !!.. itz obvious u did not understang tiss
Aman Singh
Nice One. Now i understood the whole concept clearly..!!!
Mihir
amazing tutorials!
thanks a lot for this!
joejoe
shattap virgin .
Drone
I like it but I tend to dislike syntaxy type questions. I know MANY amazing
programmers with an innate ability to create tight, highly efficient algorithms but
ask them about Big O Notation and they will literally draw a blank as-in: theyll
look at you like an alien. You see this alot in college (vs university) educated
and/or self-taught techs myself included.
Because of this fact I would rather ask them to SOLVE a problem, then explain to
me in their words WHY they chose that solution. In the end, they will give an
explanation similar to yours, without ever mentioning this specific jargon (i.e.
theyll explain how the alternative requires 100,000 loops vs. their 10,000 without
mention of Big O Notation).
Its for this same reason that I tend to look beyond syntax when quizing. As I see
it, any monkey can memorize syntax, but it requires a brain to logically approach
and solve problems!
mansi
Nice like it.. Thanks
Sarvesh Singh
awsum

Thx so much

Chirag
Very Useful..Thanks you!!!
joejoe
shattap Nerd ..
eriksala
I needed to review this for my class this summer. Thanks.

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

8 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

sinatra
I didnt even understand a thing
joejoe
vekoz u is stupid .. unlike mi that have a lot of IQ .
want some fagg ..?
Karthik
You made me realize how stupid I was to think that big O is some kind of rocket
science. Thank you, very well explained, love the way you present your case.
mwabini
fantastic, just fantastic. it really has helped me. Keep up!
alex
not only extremely useful but pleasant to read
amazing site
wit <3 from russia
vivek
good explanation.
Sachin
Nice tutorial. Now I dont fear of Big O any more.
joejoe
but you still me Me
joejoe
*fear
Marshell
Clear explanation .. like it !!
Sanaya Khan
niceeeeeeeeeee
joejoe
niceeeee tits
Suriya Shanmugam
A vivid explanation that I never heard off. Gratitude to you
nick
this is wrong "Big O Notation, also known as Big Omega Notation" Big O is upper
bound while Big Omega is lower bound
Varoon
You're right Nick, it's been fixed. Thanks!
anonfromukstudyingcs
Dude.. Thanks for this clear explanation makes more sense now to me!
saurabh
amazing explanation..thanks for this article
David Gao
I probably will attend an technical interview next week, and it is so lucky that I
found this website, very useful information.
Vuyani Billy Nyathikazi
thanks.this is very useful..i always visit this site and wow..i like the
tutorialsvery easy to understand and straight forward
darshan jadiye

06-Jul-15 9:17 AM

Big O Notation Explained with an Example

9 of 9

http://www.programmerinterview.com/index.php/data-structures/big-o-no...

thank you sir.it is so use full.clear about the all the things!
charan
very usefulkeep going..:)
ITISHA JAIMAN
got everything right it's really very useful!!!!!!!!
Sadaf
Very helpful for initial understanding
sumit
Very useful tutorial.Thank you very much Sir.
varoon10
Glad it helped you, thanks Sumit!
joejoe
Bullshit

Would you like to thank ProgrammerInterview.com for being a helpful free resource? Then why not tell a friend about us, or simply add a link
to this page from your webpage using the HTML below.
Link to this page: <a href="http://www.programmerinterview.com/index.php/data-structures/big-o-notation/">Programmer and Software Interview Questions and Ans
Please bookmark with social media, your votes are noticed and appreciated:

Copyright 2015 | Programmer Job Board | India Job Board for Programmers | About

Like 23,901 people like this. Be the first of your friends.

Website Designed by NayaPixel.com

06-Jul-15 9:17 AM

S-ar putea să vă placă și