Vous êtes sur la page 1sur 7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

WhatonearthisBigO?
BigOisthewayofmeasuringtheefficiencyofanalgorithmandhowwellitscales
basedonthesizeofthedataset.Imagineyouhavealistof10objects,andyouwant
tosorttheminorder.Theresawholebunchofalgorithmsyoucanusetomakethat
happen,butnotallalgorithmsarebuiltequal.Somearequickerthanothersbutmore
importantlythespeedofanalgorithmcanvarydependingonhowmanyitemsits
dealingwith.BigOisawayofmeasuringhowanalgorithmscales.BigOreferences
howcomplexanalgorithmis.
BigOisrepresentedusingsomethinglikeO(n).TheOsimplydenotedweretalking
aboutbigOandyoucanignoreit(atleastforthepurposeoftheinterview).nisthe
thingthecomplexityisinrelationtoforprogramminginterviewquestionsthisisalmost
alwaysthesizeofacollection.Thecomplexitywillincreaseordecreaseinaccordance
withthesizeofthedatastore.

BelowisalistoftheBigOcomplexitiesinorderofhowwelltheyscalerelativetothe
dataset.
O(1)/ConstantComplexity:Constant.Thismeansirrelevantofthesizeofthedata
setthealgorithmwillalwaystakeaconstanttime.1itemtakes1second,10items
takes1second,100itemstakes1second.Italwaystakesthesameamountoftime.
O(logn)/LogarithmicComplexity:Notasgoodasconstant,butstillprettygood.The
timetakenincreaseswiththesizeofthedataset,butnotproportionatelyso.This
meansthealgorithmtakeslongerperitemonsmallerdatasetsrelativetolargerones.
1itemtakes1second,10itemstakes2seconds,100itemstakes3seconds.Ifyour
datasethas10items,eachitemcauses0.2secondslatency.Ifyourdatasethas100,it
onlytakes0.03secondsextraperitem.Thismakeslognalgorithmsveryscalable.
O(n)/LinearComplexity:Thelargerthedataset,thetimetakengrows
proportionately.1itemtakes1second,10itemstakes10seconds,100itemstakes
100seconds.
O(nlogn):Anicecombinationoftheprevioustwo.Normallytheres2partstothe

http://www.corejavainterviewquestions.com/idiotsguidebigo/

1/7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

sort,thefirstloopisO(n),thesecondisO(logn),combiningtoformO(nlogn)1item
takes2seconds,10itemstakes12seconds,100itemstakes103seconds.
O(n^2)/QuadraticComplexity:Thingsaregettingextraslow.1itemtakes1second,
10itemstakes100,100itemstakes10000.
O(2^n):ExponentialGrowth!Thealgorithmtakestwiceaslongforeverynew
elementadded.1itemtakes1second,10itemstakes1024seconds,100items
takes1267650600228229401496703205376seconds.
Itisimportanttonoticethattheaboveisnotorderedbythebesttoworstcomplexity.
Thereisnobestalgorithm,asitcompletelyhingesonthesizeofthedatasetandthe
taskathand.Itisalsoimportanttorememberthecodecostamorecomplex
algorithmmayresultinanincrediblyquicksort,butifthecodehasbecome
unmaintainbleanddifficulttodebugisthattherightthingtodo?Itprobablydepends
onyourrequirements.
Thereisalsoavariationincomplexitywithintheabovecomplexities.Imaginean
algorithmwhichloopsthroughalistexactlytwotimes.ThiswouldbeO(2n)
complexity,asitsgoingthroughyourlistslength(n)twice!

Whydoesthismatter?
Simplyput:analgorithmthatworksonasmalldatasetisnotguaranteedtoworkwell
onalargedataset.Justbecausesomethingislighteningfastonyourmachinedoesnt
meanthatitsgoingtoworkwhenyouscaleuptoaseriousdataset.Youneedto
understandexactlywhatyouralgorithmisdoing,andwhatitsbigOcomplexityis,in
ordertochoosetherightsolution.
Therearethreethingswecareaboutwithalgorithms:bestcase,worstcaseand
expectedcase.Inrealityweonlyactuallycareaboutthelattertwo,aswereabunch
ofpessimists.Ifyouaskanalgorithmtosortapresortedlistitsprobablygoingtodoit
muchfasterthanacompletelyjumbledlist.Insteadwewanttoknowwhattheworst
case(theabsolutelymaximumamountofstepsthealgorithmcouldtake)andthe
expectedcase(thelikelyoraveragenumberofstepsthealgorithmcouldtake).Justto
addtothefun,thesecanandoftenaredifferent.

http://www.corejavainterviewquestions.com/idiotsguidebigo/

2/7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

Examples
Hopefullyyourewithmesofar,butletsdiveintosomeexamplealgorithmsforsorting
andsearching.Theimportantthingistobeabletoexplainwhatcomplexityan
algorithmis.Interviewerslovetogetcandidatestodesignalgorithmsandthenask
whatthecomplexityofitis.

O(1)
Irrelevantofthesize,itwillalwaysreturnatconstantspeed.ThejavadocforQueue
statesthatitisconstanttimefortheretrievalmethods( peek , element ,
and size ).Itsprettyclearwhythisisthecase.Forpeek,wearealwaysreturningthe
firstelementwhichwealwayshaveareferencetoitdoesntmatterhowmany
elementsfollowit.Thesizeofthelistisupdateduponelementaddition/removal,and
referencingthisnumberisjustoneoperationtoaccessnomatterwhatthesizeofthe
listis.

O(logn)
TheclassicexampleisaBinarysearch.Youreamassivegeeksoyouveobviously
alphabetisedyourmoviecollection.TofindyourcopyofBackToTheFuture,youfirst
gotothemiddleofthelist.YoudiscoverthemiddlefilmisMeetTheFockers,soyou
thenheadtothemovieinbetweenthestartandthisfilm.YoudiscoverthisisChildren
ofMen.Yourepeatthisagainandyouvefoundbacktothefuture.Theresagreat
interactivedemoofbinarysearchhere.
Althoughaddingmoreelementswillincreasetheamountoftimeittakestosearch,it
doesntdosoproportionally.ThereforeitisO(logn).

O(n)
Asdiscussedinmypostoncollections,LinkedListsarenotsogood(relatively
speaking)whenitcomestoretrieval.ItactuallyhasaBigOofO(n)asintheworst
case,tofindanelementTwhichisthelastelementinthelist,itisnecessaryto
navigatetheentirelistofnelementstofindT.Asthenumberofelementsincreasesso
doestheaccesstimeinproportion.

http://www.corejavainterviewquestions.com/idiotsguidebigo/

3/7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

O(nlogn)
ThebestexampleofO(nlogn)isamergesort.Thisisadivideandconquer
algorithm.Imagineyouhavealistofintegers.Wedividethelistintwoagainandagain
untilweareleftwithwithanumberoflistswith1itemin:eachoftheselistsis
thereforesorted.Wethenmergeeachlistwithitsneighbour(comparingthefirst
elementsofeacheverytime).Werepeatthiswiththenewcompositelistuntilwehave
oursortedresult.

(Imagefromwikipedia)
ToexplainwhythisisO(nlogn)isabitmorecomplex.Intheaboveexampleof8
numbers,wehave3levelsofsorting:
4listsortswhenthelistsizesare2
2listsortswhenthelistsizesare4
1listsortwhenthelistsizeis8
NowconsiderifIweretodoublethenumberofelementsto16:thiswouldonlyrequire
onemorelevelofsorting.Hopefullyyourrecognisethisisalognscale.
However,oneachlevelofsortingatotalofnoperationstakesplace(lookatthered
boxesinthediagramabove).thisresultsin(n*logn)operations,e.g.O(nlogn).

O(n^2)
TheBubbleSortalgorithmiseveryonesfirstalgorithminschool,andinterestinglyitis
quadraticcomplexity.Ifyouneedareminderwegothroughthelistandcompareeach
elementwiththeonenexttoit,swappingtheelementsiftheyareoutoforder.Atthe

http://www.corejavainterviewquestions.com/idiotsguidebigo/

4/7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

endofthefirstiteration,wethenstartagainfromthebeginning,withthecaveatthatwe
nowknowthelastelementiscorrect.
ImagefromWikipedia.
Imaginewritingthecodeforthisitstwoloopsofniterations.
01.

publicint[]sort(int[]toSort){

02.

for(inti=0i<toSort.length1i++){

03.

booleanswapped=false

04.

for(intj=0j<toSort.length1ij++){

05.

if(toSort[j]>toSort[j+1]){

06.

swapped=true

07.

intswap=toSort[j+1]

08.

toSort[j+1]=toSort[j]

09.

toSort[j]=swap

10.

11.

12.

if(!swapped){

13.

break

14.

15.

}returntoSort}

Thisisalsoagoodexampleofbestvsworstcase.Ifthelisttosortisalreadysorted,
thenitwillonlytakentosortasingleiterationwilldo.However,intheworstcase
wherewehavetogothroughthelistntimesandeachtimeloopinganothern(first
loop)whichisslow.
Youmaynoticethatitstechnicallylessthann2asthesecondloopdecreaseseach
time.Thisgetsignoredbecauseasthesizeofthedatasetincreasesthisimpactofthis
becomesmoreandmoremarginalandtendstowardsquadratic.

O(2^n)
Exponentialgrowth!Anyalgorithmwhereaddinganotherelementdramatically
increasestheprocessingtime.TakeforexampletryingtofindcombinationsifIhavea
listof150peopleandIwouldliketofindeverycombinationofgroupingseveryoneby
themselves,allofthegroupsof2people,allofthegroupsof3peopleetc.Usinga
simpleprogramwhichtakeseachpersonandloopsthroughthecombinations,ifIadd

http://www.corejavainterviewquestions.com/idiotsguidebigo/

5/7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

oneextrapersonthenitsgoingtoincreasetheprocessingtimeexponentially.Every
newelementwilldoubleprocessingtime.
InrealityO(2^n)areinnowayscalable.

HowtofigureoutBigOinaninterview
ThisisnotanexhaustivelistofBigO.MuchasyoucanO(n2),youcanalso
haveO(n^3)(imaginethrowinganextraloopintoyourbubblesortfornoapparent
reason).Whatthelistonthispageshouldallowyoutodoishaveastabinthedarkat
figuringoutwhatthebigOofanalgorithmis.Ifsomeoneisaskingyouthisduringan
interviewtheyprobablywanttoseehowyoutryandfigureitout.Breakdowntheloops
andprocessing.
Doesithavetogothroughtheentirelist?Therewillbeannintheresomewhere.
Doesthealgorithmsprocessingtimeincreaseataslowerratethanthesizeofthe
dataset?Thentheresprobablyalogninthere.
Aretherenestedloops?Youreprobablylookingatn^2orn^3.
Isaccesstimeconstantirrelevantofthesizeofthedataset??O(1)

SampleQuestions
Ihaveanarrayofthenumbers1to100inarandomnumber.Oneofthenumbers
ismissing.Writeanalgorithmtofigureoutwhatthenumberisandwhatposition
ismissing.
Therearemanyvariationsofthisquestionallofwhichareverypopular.Tocalculate
themissingnumberwecansimplyaddupallthenumberswedohave,andsubtract
thisfromtheexpectedanswerofthesumofallnumbersbetween1and100.Todo
thiswehavetoiteratethelistonce.Whilstdoingthiswecanalsonotewhichspothas
thegap.
1.

publicvoidfindTheBlank(int[]theNumbers){

2.

intsumOfAllNumbers=0;

3.

intsumOfNumbersPresent=0;

4.

intblankSpace=0;

5.
6.

for(inti=0;i<theNumbers.length;i++){

http://www.corejavainterviewquestions.com/idiotsguidebigo/

6/7

10/12/2015

TheIdiotsGuidetoBigO|CoreJavaInterviewQuestions

7.

sumOfAllNumbers+=i+1;

8.

sumOfNumbersPresent+=theNumbers[i];

9.

if(theNumbers[i]==0)

10.

blankSpace=i;

11.

12.

System.out.println("Missingnumber="+(sumOfAllNumberssumOfNumbersPresent)+"at
location"+blankSpace+"ofthearray");

13.

14.
15.

newBubbleSort().findTheBlank(newint[]{7,6,0,1,3,2,4});

16.
17.

//Missingnumber=5atlocation2ofthearray

Caveat:youcanalsocalculatesumOfAllNumbersusing(theNumbers.length+1)*
(theNumbers.length)/2.0).Iwouldneverrememberthatinaninterviewthough.
WhatisthebigOofyouralgo?
Ouralgorithmiteratesthroughourlistonce,soitssimplyO(n).

http://www.corejavainterviewquestions.com/idiotsguidebigo/

7/7

Vous aimerez peut-être aussi