You are on page 1of 5

HackingaGoogleInterviewHandout2

CourseDescription

Instructors:BillJacobsandCurtisFonger
Time:January1215,5:006:30PMin32124
Website:http://courses.csail.mit.edu/iap/interview

ClassicQuestion#4:Reversingthewordsinastring

Writeafunctiontoreversetheorderofwordsinastringinplace.

Answer:Reversethestringbyswappingthefirstcharacterwiththelastcharacter,
thesecondcharacterwiththesecondtolastcharacter,andsoon.Then,gothrough
thestringlookingforspaces,sothatyoufindwhereeachofthewordsis.Reverse
eachofthewordsyouencounterbyagainswappingthefirstcharacterwiththelast
character,thesecondcharacterwiththesecondtolastcharacter,andsoon.

Sorting

Often,aspartofasolutiontoaquestion,youwillneedtosortacollectionof
elements.ThemostimportantthingtorememberaboutsortingisthatittakesO(n
logn)time.(Thatis,thefastestsortingalgorithmforarbitrarydatatakesO(nlogn)
time.)

MergeSort:

Mergesortisarecursivewaytosortanarray.First,youdividethearrayinhalfand
recursivelysorteachhalfofthearray.Then,youcombinethetwohalvesintoa
sortedarray.Soamergesortfunctionwouldlooksomethinglikethis:

int[] mergeSort(int[] array) {


if (array.length <= 1)
return array;
int middle = array.length / 2;
int firstHalf = mergeSort(array[0..middle - 1]);
int secondHalf = mergeSort(
array[middle..array.length - 1]);
return merge(firstHalf, secondHalf);
}

Thealgorithmreliesonthefactthatonecanquicklycombinetwosortedarraysinto
asinglesortedarray.Onecandosobykeepingtwopointersintothetwosorted

arrays.Onerepeatedlyaddsthesmallerofthetwonumberspointedtotothenew
arrayandadvancesthepointer.

Quicksort:

Quicksortisanothersortingalgorithm.IttakesO(n^2)timeintheworstcaseand
O(nlogn)expectedtime.

Tosortanarrayusingquicksort,onefirstselectsarandomelementofthearrayto
bethe"pivot".Onethendividesthearrayintotwogroups:agroupofelementsthat
arelessthanthepivotandagroupofelementsthataregreaterthanthepivot.After
this,therewillbeanarrayconsistingofelementslessthanthepivot,followedbythe
pivot,followedbyelementsgreaterthanthepivot.Then,onerecursivelysortsthe
portionofthearraybeforethepivotandtheportionofthearrayafterthepivot.A
quicksortfunctionwouldlooklikethis:

void quicksort(int[] array, int startIndex, int endIndex) {


if (startIndex >= endIndex) {
// Base case (array segment has 1 or 0 elements
} else {
int pivotIndex = partition(array,
startIndex,
endIndex);
quicksort(array, startIndex, pivotIndex - 1);
quicksort(array, pivotIndex + 1, endIndex);
}
}

Quicksortistypicallyveryfastinpractice,butrememberthatithasO(n^2)worst
caserunningtime,sobesuretomentionanothersortingalgorithm,suchasmerge
sort,ifyouneedguaranteedO(nlogn)runningtime.

OrderStatistics:

Sometimes,aninterviewerwillaskyoutodescribeanalgorithmtoidentifythekth
smallestelementinanarrayofnelements.Todothis,youselectarandompivot
andpartitionthearrayasyouwouldinthequicksortalgorithm.Then,basedonthe
indexofthepivotelement,youknowwhichhalfofthearraythedesiredelementlies
in.Forexample,sayk=15andn=30,andafteryouselectyourpivotandpartition
thearray,thefirsthalfhas10elements(thehalfbeforethepivot).Youknowthat
thedesiredelementisthe4thsmallestelementinthelargerhalf.Toidentifythe
element,youpartitionthesecondhalfofthearrayandcontinuerecursively.The
reasonthatthisisnotO(nlogn)isthattherecursivepartitioncallisonlyonone
halfofthearray,sotheexpectedrunningtimeisn+(n/2)+(n/4)+(n/8)+...=
O(n).

Notethatfindingthemedianofanarrayisaspecialcaseofthiswherek=n/2.
Thisisaveryimportantpoint,asaninterviewerwilloftenaskyoutofindawayto
getthemedianofanarrayofnumbers.

Question:NearestNeighbor

Sayyouhaveanarraycontaininginformationregardingnpeople.Eachpersonis
describedusingastring(theirname)andanumber(theirpositionalonganumber
line).Eachpersonhasthreefriends,whicharethethreepeoplewhosenumberis
nearesttheirown.Describeanalgorithmtoidentifyeachperson'sthreefriends.

Goodanswer:Sortthearrayinascendingorderofthepeople'snumber.Foreach
person,checkthethreepeopleimmediatelybeforeandafterthem.Theirthree
friendswillbeamongthesesixpeople.ThisalgorithmtakesO(nlogn)time,since
sortingthepeopletakesthatmuchtime.

LinkedLists

Alinkedlistisabasicdatastructure.Eachnodeinalinkedlistcontainsanelement
andapointertothenextnodeinthelinkedlist.Thelastnodehasa"null"pointerto
indicatethatthereisnonextnode.Alistmayalsobedoublylinked,inwhichcase
eachnodealsohasapointertothepreviousnode.Ittakesconstant(O(1))timeto
addanodetoorremoveanodefromalinkedlist(ifyoualreadyhaveapointerto
thatnode).IttakesO(n)timetolookupanelementinalinkedlistifyoudon't
alreadyhaveapointertothatnode.

ClassicQuestion#5:CycleinaLinkedList

Howcanonedeterminewhetherasinglylinkedlisthasacycle?

Goodanswer:Keeptrackoftwopointersinthelinkedlist,andstartthematthe
beginningofthelinkedlist.Ateachiterationofthealgorithm,advancethefirst
pointerbyonenodeandthesecondpointerbytwonodes.Ifthetwopointersare
everthesame(otherthanatthebeginningofthealgorithm),thenthereisacycle.If
apointereverreachestheendofthelinkedlistbeforethepointersarethesame,
thenthereisnocycle.Actually,thepointersneednotmoveoneandtwonodesata
time;itisonlynecessarythatthepointersmoveatdifferentrates.ThistakesO(n)
time.Thisisatrickyanswerthatinterviewersreallylikeforsomereason.

Okayanswer:Foreverynodeyouencounterwhilegoingthroughthelistonebyone,
putapointertothatnodeintoaO(1)lookuptimedatastructure,suchasahashset.
Then,whenyouencounteranewnode,seeifapointertothatnodealreadyexistsin
yourhashset.ThisshouldtakeO(n)time,butalsotakesO(n)space.

Okayanswer:Gothroughtheelementsofthelist."Mark"eachnodethatyoureach.
Ifyoureachamarkednodebeforereachingtheend,thelisthasacycle;otherwise,it
doesnot.ThisalsotakesO(n)time.

Notethatthisquestionistechnicallyillposed.Anordinarylinkedlistwillhaveno
cycles.Whattheyactuallymeanisforyoutodeterminewhetheryoucanreacha
cyclefromanodeinagraphconsistingofnodesthathaveatmostoneoutgoing
edge.

StacksandQueues

Aninterviewerwillprobablyexpectyoutoknowwhatqueuesandstacksare.
Queuesareabstractdatatypes.Aqueueisjustlikealineofpeopleatanamusement
park.Aqueuetypicallyhastwooperations:enqueueanddequeue.Enqueueingan
elementaddsittothequeue.Dequeueinganelementremovesandreturnsthe
elementthatwasaddedleastrecently.AqueueissaidtobeFIFO(firstin,firstout).

Astackisanotherabstractdatatypewithtwocommonoperations:pushandpop.
Pushinganelementaddsittothestack.Poppinganelementremovesandreturns
theelementthatwasaddedmostrecently.AstackissaidtobeLIFO(lastin,first
out).Astackoperateslikeastackofcafeteriatrays.

HashTables

Ahashtableisusedtoassociatekeyswithvalues,sothateachkeyisassociatedwith
oneorzerovalues.Eachkeyshouldbeabletocomputea"hash"function,which
takessomeorallofitsinformationanddigestsitintoasingleinteger.Thehash
tableconsistsofanarrayofhashbuckets.Toaddakeyvaluepairtoahashtable,
onecomputesthekey'shashcodeandusesittodecidethehashbucketinwhichthe
mappingbelongs.Forexample,ifthehashvalueis53andthereare8hashbuckets,
onemightusethemodfunctiontodecidetoputthemappinginbucket53mod8,
whichisbucket5.Tolookupthevalueforagivenkey,onecomputesthebucketin
whichthekeywouldresideandcheckswhetherthekeyisthere;ifso,onecan
returnthevaluestoredinthatbucket.Toremovethemappingforagivenkey,one
likewiselocatesthekey'smappingandremovesitfromtheappropriatebucket.
Notethatthehashfunctionisgenerallydecidedoninadvance.

Aproblemariseswhentwokeyshashtothesamebucket.Thiseventiscalleda
"collision".Thereareseveralwaystodealwiththis.Onewayistostorealinkedlist
ofkeyvaluepairsforeachbucket.

Insertion,removal,andlookuptakeexpectedO(1)time,providedthatthehash
functionissufficiently"random".Intheworstcase,eachkeyhashestothesame
bucket,soeachoperationtakesO(n)time.Inpractice,itiscommontoassume
constanttime.

Hashtablescanoftenbeusedassmallercomponentsofanswerstoquestions.In
ourexperience,someinterviewerslikehashtablesandsomedon't.Thatis,some
interviewerswillallowyoutoassumeconstanttime,whileotherswillnot.Ifyou
wanttouseahashtable,werecommendsubtlytryingtofigureoutwhichcategory
yourinterviewerbelongsto.Youmight,forexample,saysomethinglike,"Well,I
couldusedahashtable,butthatwouldhavebadworstcaseperformance."The
interviewermightthenindicatethathe'llallowyoutouseahashtable.

ClassicQuestion#6:Datastructureforanagrams

GivenanEnglishwordintheformofastring,howcanyouquicklyfindallvalid
anagramsforthatstring(allvalidrearrangementsofthelettersthatformvalid
Englishwords)?Youareallowedtoprecomputewhateveryouwanttoandstore
whateveryouoptionallyprecomputeondisk.

Answer:Wewanttouseahashtable!Ifyourinterviewerreallyhateshashtables
(whichtheysometimesdoforsomereason),youcanuseatreeinstead.Butlet's
assumeyoucanuseahashtable.Thenfortheprecomputingstep,gothrougheach
wordinthedictionary,sortthelettersofthewordinalphabeticalorder(so
"hacking"wouldbecome"acghikn")andaddthesortedlettersasakeyinthetable
andtheoriginalwordasoneofthevaluesinalistofvaluesforthatkey.For
example,theentryfor"opst"wouldbethelist["opts","post","stop","pots","tops",
"spot"].Then,wheneveryougetastring,yousimplysortthelettersofthestring
andlookupthevalueinthehashtable.TherunningtimeisO(nlogn)forsorting
thestring(whichisrelativelysmall)andapproximatelyO(1)forthelookupinthe
hashtable.

Thereareseveralotherpossibleanswerstothisquestion,butwefeelthatthe
answeraboveisconsideredanoptimalsolution.

Question:FactorialZeros

Withoutusingacalculator,howmanyzerosareattheendof"100!"?(that's
100*99*98*...*3*2*1)

Answer:Whatyoudon'twanttodoisstartmultiplyingitallout!Thetrickis
rememberingthatthenumberofzerosattheendofanumberisequaltothe
numberoftimes"10"(or"2*5")appearswhenyoufactorthenumber.Therefore
thinkabouttheprimefactorizationof100!andhowmany2sand5sthereare.
Thereareabunchmore2sthan5s,sothenumberof5sisalsothenumberof10sin
thefactorization.Thereisone5foreveryfactorof5inourfactorialmultiplication
(1*2*...*5*...*10*...*15*...)andanextra5for25,50,75,and100.Thereforewehave
20+4=24zerosattheendof100!.

You might also like