Documente Academic
Documente Profesional
Documente Cultură
1 of 9
http://www.programmerinterview.com/index.php/data-structures/big-o-no...
Translate:
Home
Java
C/C++
Databases/SQL
Javascript
PHP
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
on Big O Notation, along with some simple examples to really help you understand it. You
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.
Inorder Traversal
Postorder Traversal
efficient.
Networking
Its really not that bad at all and it is something best illustrated by an example with
Recursion
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.
Assortment of Knowledge
06-Jul-15 9:17 AM
http://www.programmerinterview.com/index.php/data-structures/big-o-no...
that array.
American Vocabulary
Science Questions
Email
Country
United States
Secure Connection
Failed
2 of 9
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.
06-Jul-15 9:17 AM
3 of 9
http://www.programmerinterview.com/index.php/data-structures/big-o-no...
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.
06-Jul-15 9:17 AM
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.
06-Jul-15 9:17 AM
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.
06-Jul-15 9:17 AM
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
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
7 of 9
http://www.programmerinterview.com/index.php/data-structures/big-o-no...
..}
}
}
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
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
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
06-Jul-15 9:17 AM