Table Of ContentTable of Contents
I The Interview 5
Getting Ready· 6
2 Strategies For·AGreat Interview· 11
3 OonductlnqAn Interview· 18
4 ProblemSolving Patterns- 22
II Problems 46
5 PrimitiveTypes· 47
6 ArraysandStrings· 52
7 LinkedLists· 62
8 StacksandQueues .67
9 BinaryTrees· 73
10 Heaps· 80
11 Searching· 84
12 HashTables· 92
13 Sorting' 98
14 BinarySearchTrses-104
15 Meta-algorithms ·114
16 Algorithms onGraphs· 130
17 Intractability· 138
18 ParallelComputing ·144
19 Design Problems·150
20 Probability· 155
21 DiscreteMathematics' 163
III Solutions 171
IV Notation and Index 469
IndexofTerms· 472
Introduction
And itought toberemembered tluJtthereisnolhing ,"em!
difflcuJt to tllb Inl1111fdm, em!per{W1I$ to cond'lct, or
mOI'I!ullCtrtaininUssuccess,tIIIIntot/lb theItsIdinthe
introduction ofIIneworderofthings.
- N.MACH'AVILLI,1513
Elements of Programming Interviews (EPI)aims to help engineers interviewing
for software development positions. Theprimary focusof EPIis data structures,
algorithms,systemdesign,and problemsolving. Thematerialislargelypresented
throughquestions.
Aninterview problem
Let'sbeginwithFigure1below.Itdepictsmovementsinthesharepriceofacompany
over40days. Specifically,foreachday,thechartshowsthedailyhighand low,and
thepriceattheopeningbell(denotedbythewhitesquare).Supposeyouwereasked
in an interview to design an algorithm that determines the maximum profit that
could havebeen madebybuying and thensellingasingleshareoveragivenday
range,subjecttotheconstraintthatthebuyandthesellhavetotakeplaceatthestart
oftheday.(Thisalgorithmmaybeneededtobacktestatradingstrategy.)
Youmaywant tostopreadingnow,andattempt thisproblemonyourown.
Firstclarify the problem. For example, you should ask for the input format.
Let'ssaytheinput consistsofthreearraysL,H,and 5,ofnonnegative floatingpoint
numbers,representingthelow,high,andstartingpricesforeachday.Theconstraint
that the purchase and salehave totake placeat the start of the day means that it
Da:y0 Day5 Day10 Day15 Day20 Day25 Day30 Day35 Day40
Figure 1:Sharepriceas afunctionoftime.
1
2 Introduction
sufficestoconsider S. Youmaybe tempted tosimply return the differenceofthe
minimumandmaximumelementsinS.Ifyou try afewtestcases,youwillseethat
the minimum canoccurafterthe maximum,whichviolates therequirement inthe
problemstatement-you havetobuybeforeyoucansell.
At this point, a brute-forcealgorithm would be appropriate. Foreach pair of
=
indices i and j > i compute Pi,} S[]1- S[i] and compare this differenceto the
largestdifference,d, seenso far. IfPi,}isgreater thand, setd toPi,i' Youshould be
ableto codethis algorithm using a pair ofnested for-loopsand testit in amatter
ofa fewminutes. Youshould alsoderiveits timecomplexityasa functionofthe
length n of the input array. The inner loop isinvoked n - 1 times, and the i-th
iteration processesn- 1- ielements. Processingan elemententails computing a
difference,performing a compare, and possibly updating a variable, allofwhich
=
takeconstanttime.Hencetheruntimeisproportionaltor,~~(n-1-k) (n-~)(n),i.e.,
the timecomplexityofthebrute-forcealgorithmisO(n2). Youshould alsoconsider
thespacecomplexity,i.e.,how muchmemoryyour algorithmuses. Thearrayitself
takesmemoryproportional ton,andtheadditionalmemoryusedbythebrute-force
algorithmisaconstantindependent ofn-a coupleofiterators andonetemporary
floatingpointvariable.
Onceyou have a working algorithm, try to improve upon it. Specifically,an
O(n2) algorithmisusually not acceptablewhen facedwith largearrays. Youmay
haveheard ofan algorithmdesignpattern calleddivide and conquer. Ityieldsthe
followingalgorithm for this problem. Split S into two subarrays, S[O: LV] and
S[L¥J+1 :n- 1];compute the best result forthe firstand secondsubarrays; and
combinetheseresults. Inthe combinestep wetakethebetter ofthe resultsforthe
twosubarrays. However,wealsoneedtoconsiderthecasewheretheoptimumbuy
and selltakeplaceinseparate subarrays. Whenthisisthe case,thebuy must bein
the firstsubarray,and the sellin the secondsubarray,sincethebuy must happen
beforethesell.Iftheoptimumbuy andsellareindifferentsubarrays,theoptimum
buypriceistheminimumpriceinthefirstsubarray,andtheoptimumsellpriceisin
themaximumpriceinthesecondsubarray.WecancomputethesepricesinO(n)time
with asinglepass overeachsubarray. Thereforethe timecomplexityT(n) forthe
divide andconquer algorithmsatisfiesthe recurrencerelationT(n) = 2T(V +O(n),
whichsolvestoO(nlogn).
Thedivideandconqueralgorithmiselegantandfast.Itsimplementationentails
somecornercases,e.g.,anemptysubarray,subarraysoflengthone,and anarrayin
whichtheprice decreasesmonotonically,but it canstillbe written and testedbya
gooddeveloperin20-30minutes.
Lookingcarefullyatthecombinestepofthe divideand conqueralgorithm,you
mayhave a flashofinsight. Specifically,you maynoticethat themaximum profit
thatcanbemade bysellingonaspecificdayisdetermined bytheminimum ofthe
stockpricesoverthepreviousdays. Sincethemaximumprofitcorrespondstoselling
onsomeday,thefollowingalgorithmcorrectlycomputesthemaximumprofit.Iterate
throughS,keepingtrackoftheminimumelementmseenthusfar.Ifthedifferenceof
thecurrentelementandmisgreaterthanthemaximumprofitrecordedsofar,update
ElementsOfProgranuninglntervietls. com
Introduction 3
the maximum profit. This algorithm performs aconstant amount ofwork per array
element, leading to an O(n)time complexity. Ituses two float-valued. variables (the
minimum element and the maximum profit recorded so far) and an iterator, i.e.,
0(1) additional space. Itis considerably simpler to implement than the divide and
conquer algorithm-a few minutes should suffice towrite and test it. Working code
ispresented inSolution 6.3onPage 185.
Ifin a45-60 minutes interview, you can develop the algorithm described. above,
implement and test it, and analyze its complexity, you would have had avery suc
cessful interview. Inparticular, you would have demonstrated to your interviewer
that you possess several key skills:
- The ability to rigorously formulate real-world problems.
- The skills tosolve problems and design algorithms.
- The tools to gofrom an algorithm toa tested program.
- The analytical techniques required todetermine the computational complexity
ofyour solution.
Book organization and study guide
Interviewing successfully is about more than being able to intelligently select data
structures and design algorithms quickly. For example, you also need toknow how
toidentify suitable companies, pitch yourself, ask forhelp when you are stuck onan
interview problem, and convey your enthusiasm. These aspects of interviewing are
the subject of Chapters 1-3, and are summarized inTable 1.1on Page 7.
Chapter 1isspecifically concerned with preparation, Chapter 2discusses how you
should conduct yourself atthe interview itself and Chapter 3describes interviewing
fromtheinterviewer's perspective. Thelatter isimportant forcandidates too,because
ofthe insights itoffers into the decision making process. Chapter 4reviews problem
solving patterns.
Sincenot everyone willhave the time to work through EPIinits entirety, we have
prepared astudy guide (Table1.2onPage 8)toproblems you should solve, based on
the amount oftime you have available.
Theproblem chapters areorganized asfollows. Chapters 5-14 areconcerned with
basic data structures, such as arrays and binary search trees, and basic algorithms,
such as binary search and quicksort. In our experience, this is the material that
most interview questions are based on. Chapters 15-1.7cover advanced algorithm
design principles, such as dynamic programming and heuristics, as well as graphs.
Chapters 18-19focus ondistributed and parallel programming, and design problems.
Chapters 20-21 study probability and discrete mathematics: candidates for positions
in finance companies should pay special attention tothem.
The notation, specifically the symbols we use for describing algorithms, e.g.,
ISI,A[i: j], isfairly standard. Itissummarized starting onPage 470iyou are strongly
recommended to review it. Terms, e.g., BPSand dequeue, are indexed starting on
Page 471.
ElementsOfProgramminglnterviews.com
4 Introduction'
Problems, solutions, variants, and ninjas
Most solutions inEPIarebased onbasic concepts, such asarrays, hash tables, and bi
nary search, used inclever ways. Afewsolutions use relatively advanced machinery,
e.g.,Dijkstra's shortest path algorithm or random variables. Youwill encounter such
problems in an interview only ifyou have a graduate degree or claim specialized
knowledge, such asgraph theory or randomized algorithms.
Most solutions include code snippets. These areprimarily written inC++, and use
C++l1 features. Programs concerned with concurrency are inJava. C++l1 features
germane to EPI are reviewed on Page 172. A guide to reading C++ programs for
Java developers isgiven on Page 172.Source code, which includes randomized and
directed test cases, can be found at ElementsOfProgrammlnginterviews. com/code.
System design problems, and some problems related to probability and discrete
mathematics, are conceptual and not meant to be coded.
At the end of many solutions we outline problems that are related to the original
question. We classify such problems as variants and s-variants. A variant is a
problem whose formtilation or solution is similar to the solved problem. An e
variant isaproblem whose solution differs slightly, ifat all,from the given solution.
Some s-variants may be phrased quite differently from the original problem.
Approximately aquarter of the questions inEPIhave a white ninja (~) or black
ninja (0')designation. White ninja problems aremore challenging, and aremeant for
applicants from whom thebar ishigher, e.g.,graduate students and tech leads. Black
ninja problems are exceptionally difficult, and are suitable fortesting a candidate's
response to stress, as described on Page 16. Non-ninja questions should be solvable
within an hour-long interview and, insome cases, take substantially less time.
Leveland prerequisites
Weexpect readers to be familiar with data structures and algorithms taught at the
undergraduate level. Thechapters onconcurrency and system design require knowl
edge of locks, distributed systems, operating systems (OS),and insight into com
monly used applications. Much ofthe material in the chapters on meta-algorithms,
graphs, intractability, probability, and discrete mathematics is more advanced and
geared towards candidates with graduate degrees or specialized knowledge.
The review at the start of each chapter isnot meant to be comprehensive and if
you are not familiar with the material, you should first study it in an algorithms
textbook. There are dozens ofsuch texts and our preference is to master one or two
good books rather than superficially sample many. WelikeAlgorithmsby Dasgupta,
Papadimitriou, and Vazirani because itissuccinct and beautifully written; Introduc
tiontoAlgorithmsbyCormen, Leiserson, Rivest, and Stein ismore detailed and serves
as agood reference.
Since our focus is on problems that can be solved in an interview, we do not
include many elegant algorithm design problems. Similarly, we do not have any
straightforward review problems; you may want to brush up on these using text
books.
ElementsOfProgrammingInterviews.com
Part I
The Interview
CHAPTBR
Getting Ready
Beforeeverything else,getting readyisthesecretofsuccess,
-H.PoRD
Themostimportant partofinterviewpreparationisknowingthematerialandprac
ticingproblemsolving. However thenontechnicalaspectsofinterviewingare also
very important, and oftenoverlooked. Chapters 1-3 areconcernedwith the non
technicalaspectsofinterviewing, ranging from resumepreparation tohow hiring
decisionsaremade. Theseaspectsofinterviewingaresummarized inTableLIon
thefacingpage
Study guide
Ideally,youwouldprepare foraninterviewbysolvingalltheproblemsinEPI.This
isdoableover12monthsifyousolveaproblemaday,wheresolvingentailswriting
aprogramand gettingittoworkonsometestcases.
Sincedifferentcandidateshave differenttimeconstraints,wehaveoutlined sev
eralstudy scenarios,andrecommendedasubsetofproblemsforeachscenario.This
information issummarized inTable1.2 on Page8. Thepreparation scenarioswe
consider are Hackathon (aweekend entirely devoted topreparation), finalscram
(oneweek,3-4 hoursperday),termproject(fourweeks,1.5-2.5hoursper day),and
algorithmsclass(3-4months,1hourper day).
At Coogle,Amazon, Microsoft,and similarcompanies, a largemajority of the
interviewquestionsaredrawn fromthetopicsinChapters5-14. Exercisecommon
sensewhenusingTable1.2(e.g.,ifyouareinterviewingforapositionwithafinancial
firm,youshouldpaymoreemphasistoProbabilityandDiscreteMathematics.Ifyou
have a graduate degree or are interviewing fora lead position, add somestarred
problems.
Although aninterviewermayoccasionallyaskaquestiondirectlyfromEPr,you
shouldnotbaseyourpreparationonmemorizingsolutions. Rotelearningwilllikely
leadtoyourgivingaperfectsolutiontothewrongproblem.
6
Chapter 1. GettingReady 7
Table 1.1: Asummary ofnontechnical aspectsofinterviewing
The Interview Lifecycle, on the current At the IntervIew, onPage 11
page - Don't solve thewrong problem
- Identify companies, contacts - Get specs &requirements
- Resume preparation - Construct sample input/output
o Basicprinciples - Workon small examples first
o Website with links toprojects - Spell out the brute-force solution
o LinkedIn profile &recommendations - Think out loud
- Resumesubmission - Apply patterns
- Mock interview practice - Test forcomer-cases
- Phone/campus screening - Use proper syntax
- On-site interview - Manage thewhiteboard
- Negotiating an offer - Beaware ofmemory management
- Getfunction Signaturesright
General Advice, on Page 15 Conducting an Interview, on Page 18
- Know thecompany &interviewers - Don't be indecisive
- Communicate clearly - Create abrand ambassador
- Bepassionate - Coordinate with other interviewers
- Behonest o know what toteston
- Stay positive o look forpatterns ofmistakes
- Don't apologize - Characteristics ofagood problem:
- Bewell-groomed e nosingle point offailure
- Mind your body language e hasmultiple solutions
- Leave perks and money out o covers multiple areas
- Beready forastress interview o iscalibrated on colleagues
- Learn from bad outcomes e does not require unnecessary domain
- Negotiate thebest offer knowledge .
- Control the conversation
o draw out quiet candidates
o manage verbose/overconfident candi-
dates
- Useaprocess forrecording &scoring
- Determine what training isneeded
- Apply the litmus test
The interview lifecyc1e
Generallyspeaking,interviewingtakesplaceinthefollowingsteps:
1. Identifycompanies that you are interested in, and, ideally,find people you
knowatthesecompanies.
2. Prepareyourresume usingtheguidelines onthefollowingpage, andsubmit
itvia apersonal contact(preferred),orthrough anonlinesubmissionprocess
oracampuscareerfair.
3. Performan initialphonescreening,whichoftenconsistsofaquestion-answer
sessionoverthephone or videochatwith anengineer. Youmaybe askedto
submitcodeviaashareddocumentoranonlinecodingsitesuchasideone.com
orcollabedit.comD. on't take thescreeningcasually-it canbeextremelychal
lenging.
4. Goforan on-siteinterview-this consistsofaseriesofone-on-oneinterviews
ElementsOfProgrammingInterviews.com
Description:The sampler should give you a very good idea of the quality and style of our book. In particular, be sure you are comfortable with the level and with our C++ coding style.Solutions include code snippets which are primarily in C++. Programs concerned with concurrency are in Java. Complete programs ar