Professional Documents
Culture Documents
PersonA
Question:Substring
Writeaprogramtodeterminewhetheraninputstringxisasubstringofanother
inputstringy.(Forexample,"bat"isasubstringof"abate",butnotof"beat".)You
mayuseanylanguageyoulike.
SampleAnswer(inC++):
Describeadesignforatexteditor.Describetheclasses,interfaces,andsoonthat
youwoulduseandhowyouwouldorganizethem.
Answer:Therearesomanypossibleanswerstothisproblemthatitwouldbe
difficulttosaythatoneansweristhebest.Looktomakesurethattheymake
classestosetupatexteditor(classesfortheGUI,formatting,saving/loadingfiles,
handlinginput,etc.).Usinginheritance(subclassinginobjectoriented
programming)whereitmakessenseisalsogoodforreusabilityandextendability.
Usingdesignpatters(suchasModelViewController,Listener/Observer,orthe
Singletonpattern)isalsoagoodthing.Themainpointisforthemtogetusedto
thinkingabouthowtheywoulddesignasystem.Mostimportantly,theyneedto
thinkaboutsimplicity,reusability,andextendabilityintheirdesign.
Atexteditordesignquestionisslightlydifferentfromotherdesignquestionsinthat
programmersoftenhavestrongfeelingsabouthowatexteditorshouldwork.
Programmersoftenwanttheabilitytogreatlymodifythebehavioroftheireditor
andwanttobeabletowriteextensionsthataddfunctionalitytoit.Themajortext
editorsusedbyprogrammerstoday,suchasEmacs,Vim,Eclipse,andVisualStudio
havethisability.Adiscussionabouthowtheirtexteditorwouldaccomplishthis
(especiallywithhowthedesignwouldincludeaplaceforextensionsandhowinput
wouldbehandled)wouldbegood.
Question:AxisAlignedRectangles
Describeanalgorithmthattakesanunsortedarrayofaxisalignedrectanglesand
returnsanypairofrectanglesthatoverlaps,ifthereissuchapair.Axisaligned
meansthatalltherectanglesidesareeitherparallelorperpendiculartothexand
yaxis.Youcanassumethateachrectangleobjecthastwovariablesinit:thexy
coordinatesoftheupperleftcornerandthebottomrightcorner.
GoodAnswer:Createasortedarrayofthexcoordinatesoftheleftandrightedgesof
therectangles.Then,usea"scanline"tomovefromlefttorightthroughthe
rectangles.Keepabinarysearchtreecontainingtheycoordinatesofthetopand
bottomedgesoftherectanglesthatoverlapthescanline.Foreachelementofthe
array,checkwhetheritisaleftorrightedge.Ifitisarightedge,removethe
correspondingtopandbottomedgesfromtheBST.Ifitisaleftedge,searchtheBST
forrectanglesthatoverlapthecurrentrectangle;ifthereisone,returntheoverlap.
Then,addtheycoordinatesofthetopandbottomedgesoftherectangletotheBST.
ThesearchtakesO(nlogn)time,sinceittakesO(nlogn)timetosorttherectangles
andeachofthe2niterationstakesO(logn)time.
Question:DoublyLinkedList
Writeafunctiontoremoveasingleoccurrenceofanintegerfromadoublylinked
listifitispresent.Youmayuseanylanguageyoulike.
SampleAnswer(inJava):
}
}
Question:MinimumStack
Describeastackdatastructurethatsupports"push","pop",and"findminimum"
operations."Findminimum"returnsthesmallestelementinthestack.
GoodAnswer:Storetwostacks,oneofwhichcontainsalloftheitemsinthestack
andoneofwhichisastackofminima.Topushanelement,pushitontothefirst
stack.Checkwhetheritissmallerthanthetopitemonthesecondstack;ifso,push
itontothesecondstack.Topopanitem,popitfromthefirststack.Ifitisthetop
elementofthesecondstack,popitfromthesecondstack.Tofindtheminimum
element,simplyreturntheelementonthetopofthesecondstack.Eachoperation
takesO(1)time.
Question:HashTables
Describehowahashtableworks.
Answer:Youcanrefertohandout2foradescriptionofhashtables.
Question:CoinFlippingandDieRolls
Describeanalgorithmtooutputadieroll(arandomnumberfrom1to6),givena
functionthatoutputsacointoss(arandomnumberfrom1to2).Eachpossible
outcomeshouldbeequallylikely.
SampleAnswer:Flipthecointhreetimes,andusethethreecoinflipsasthebitsofa
threebitnumber.Ifthenumberisintherange1to6,outputthenumber.
Otherwise,repeat.Notethatmanyotheranswersarepossible.
Question:TargetSum
Givenanintegerxandanunsortedarrayofintegers,describeanalgorithmto
determinewhethertwoofthenumbersadduptox.(Inthiscase,saythatthe
interviewerhateshashtables.)
GoodAnswer:Sortthearray.Then,keeptrackoftwopointersinthearray,oneat
thebeginningandoneattheend.Wheneverthesumofthecurrenttwointegersis
lessthanx,movethefirstpointerforwards,andwheneverthesumisgreaterthanx,
movethesecondpointerbackwards.Ifyoucannotfindtwonumbersthataddtox
beforeoneofthepointersmeet,thenthereisnopairofintegersthatsumtox.This
solutiontakesO(nlogn)timebecausewesortthenumbers.
AnotherGoodAnswer:Createabinarysearchtreecontainingxminuseachelement
inthearray.Then,checkwhetheranyelementofthearrayappearsintheBST.It
takesO(nlogn)timetocreateabinarysearchtreefromanarray,sinceittakes
O(logn)timetoinsertsomethingintoaBST,andittakesO(nlogn)timetoseeif
anyelementinanarrayisinaBST,sincethelookuptimeforeachelementinthe
arraytakesO(logn).ThereforesteponetakesO(nlogn)timeandsteptwotakes
O(nlogn)time,soourtotalrunningtimeisO(nlogn).
Question:Debugging
Describeagoodstrategytofindabuginaprogram.
Answer:Thisquestionhasmanypossibleanswers,andisthesortofopenended
questionthatinterviewersoccasionallyask.Agoodanswertothisquestionmight
includeidentifyingtheportionoftheprograminwhichthebugappearstobe
occurringbasedonitsbehavior,aswellasusingbreakpointsandasteppertostep
throughtheprogram.Anyanswersthatinvolvethinkingaboutpossiblesourcesof
theproblemandfindingwaystolimitthesearchscopeofthebugaregoodanswers.