336x Filetype PDF File size 0.67 MB Source: elementsofprogramminginterviews.com
Elements
of
Programming
Interviews in C++
TheInsiders’ Guide
AdnanAziz
Tsung-HsienLee
AmitPrakash
This document is a sampling of our book, Elements of Program-
ming Interviews in C++ (EPI). Its purpose is to provide examples
of EPI’s organization, content, style, topics, and quality. The sam-
pler focuses solely on problems; in particular, it does not include
three chapters on the nontechnical aspects of interviewing. We’d
love to hear from you—we’re especially interested in your sugges-
tions as to where the exposition can be improved, as well as any
insights into interviewing trends you may have.
You can buy EPI at Amazon.com (paperback) and Google Play
(Ebook).
http://ElementsOfProgrammingInterviews.com
AdnanAzizisaResearchScientist at Facebook. Previously, he was a professor at the Department
of Electrical and Computer Engineering at The University of Texas at Austin, where he conducted
research and taught classes in applied algorithms. He received his Ph.D. from The University of
California at Berkeley; his undergraduate degree is from Indian Institutes of Technology Kanpur.
He has worked at Google, Qualcomm, IBM, and several software startups. When not designing
algorithms, he plays with his children, Laila, Imran, and Omar.
Tsung-Hsien Lee is a Senior Software Engineer at Uber. Previously, he worked as a Software
Engineer at Google and as Software Engineer Intern at Facebook. He received both his M.S. and
undergraduate degrees from National Tsing Hua University. He has a passion for designing and
implementingalgorithms. Helikes to apply algorithms to every aspect of his life. He takes special
pride in helping to organize Google Code Jam 2014 and 2015.
AmitPrakashisaco-founderandCTOofThoughtSpot,aSiliconValleystartup. Previously,hewasa
MemberoftheTechnicalStaffatGoogle,whereheworkedprimarilyonmachinelearningproblems
that arise in the context of online advertising. Before that he worked at Microsoft in the web search
team. He received his Ph.D. from The University of Texas at Austin; his undergraduate degree is
from Indian Institutes of Technology Kanpur. When he is not improving business intelligence, he
indulges in his passion for puzzles, movies, travel, and adventures with Nidhi and Aanya.
ElementsofProgrammingInterviews: TheInsiders’Guide
byAdnanAziz,Tsung-HsienLee,andAmitPrakash
Copyright©2017AdnanAziz,Tsung-HsienLee,andAmitPrakash. Allrightsreserved.
Nopartofthispublication may be reproduced, stored in a retrieval system, or transmitted, in any
form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the
prior consent of the authors.
The views and opinions expressed in this work are those of the authors and do not necessarily
reflect the official policy or position of their employers.
A
Wetypesetthis book using LT X and the Memoir class. We used TikZ to draw figures. Allan Ytac
E
created the cover, based on a design brief we provided.
The companion website for the book includes contact information and a list of known errors for
eachversionofthebook. Ifyoucomeacrossanerrororanimprovement,pleaseletusknow.
Website: http://elementsofprogramminginterviews.com
Distributed under the Attribution-NonCommercial-NoDerivs 3.0 License
TableofContents
I DataStructuresandAlgorithms 1
1 Primitive Types 2
1.1 Computingtheparityofaword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Arrays 7
2.1 TheDutchnationalflagproblem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Strings 13
3.1 Interconvert strings and integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2 Baseconversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4 LinkedLists 17
4.1 Test for cyclicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5 Stacks and Queues 22
5.1 ImplementastackwithmaxAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2 Computebinarytreenodesinorderofincreasingdepth . . . . . . . . . . . . . . . 27
6 BinaryTrees 30
6.1 Test if a binary tree is height-balanced . . . . . . . . . . . . . . . . . . . . . . . . . 32
7 Heaps 35
7.1 Mergesortedfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8 Searching 39
8.1 Search a sorted array for first occurrence of k . . . . . . . . . . . . . . . . . . . . . 42
9 HashTables 44
9.1 Is an anonymousletter constructible? . . . . . . . . . . . . . . . . . . . . . . . . . 48
10 Sorting 50
10.1 Computetheintersectionoftwosortedarrays . . . . . . . . . . . . . . . . . . . . 52
10.2 Renderacalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
11 BinarySearchTrees 56
i
11.1 Test if a binary tree satisfies the BST property . . . . . . . . . . . . . . . . . . . . . 58
12 Recursion 61
12.1 Generatethepowerset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
13 DynamicProgramming 65
13.1 Countthenumberofwaystotraversea2Darray . . . . . . . . . . . . . . . . . . . 67
14 GreedyAlgorithmsandInvariants 71
14.1 The3-sumproblem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
15 Graphs 75
15.1 Paint a Boolean matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
16 Parallel Computing 80
16.1 ImplementaTimerclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
II DomainSpecificProblems 83
17 DesignProblems 84
17.1 Designasystemfordetectingcopyrightinfringement . . . . . . . . . . . . . . . . 85
18 LanguageQuestions 87
18.1 References and pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
19 Object-Oriented Design 88
19.1 Creational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
20 CommonTools 90
20.1 Merginginaversioncontrolsystem . . . . . . . . . . . . . . . . . . . . . . . . . . 90
III The Honors Class 94
21 HonorsClass 95
21.1 Computethegreatestcommondivisor . . . . . . . . . . . . . . . . . . . . . . . 96
21.2 Computethemaximumproductofallentriesbutone . . . . . . . . . . . . . . 97
IV NotationandIndex 100
Notation 101
IndexofTerms 103
no reviews yet
Please Login to review.