CRACKING THE CODING INTERVIEW 150 Programming Questions and Solutions Gayle Laakmann McDowell Founder / CEO, CareerCup.com CRACKING THE CODING INTERVIEW 5th Edition ALSO BY GAYLE LAAKMANN MCDOWELL THE GOOGLE RESUME How TO PREPARE FOR A CAREER AND LAND A JOB AT APPLE, MICROSOFT, GOOGLE, OR ANY TOP TECH COMPANY CRACKING THE CODING INTERVIEW 5th Edition 150 Programming Questions and Solutions GAYLE LAAKMANN MCDOWELL Founder and CEO, CareerCup.com CareerCup, LLC Palo Alto, CA CRACKING THE CODING INTERVIEW, FIFTH EDITION Copyright © 2008 - 2013 by Gayle Laakmann McDowell All rights reserved No part of this book may be reproduced in any form by any electronic or mechanical means, including information storage and retrieval systems, without permission in writing from the author or publisher, except by a reviewer who may quote brief passages in a review Published by CareerCup, LLC, Palo Alto, CA Version 5.01390210100131 No part of this book may be used or reproduced in any manner without written permission except in the case of brief quotations in critical articles or reviews For more information, contact support@careercup.com 978-0984782802 (ISBN 13) To Pauline "Oma" Venti for her eternal support Table of Contents Foreword Introduction I The Interview Process Overview How Questions are Selected Timeline and Preparation Map II III IV V VI VI The Evaluation Process 10 Incorrect Answers 11 Dress Code 12 Top 10 Mistakes 13 Frequently Asked Questions 15 Behind the Scenes 17 The Microsoft Interview 19 The Amazon Interview 20 The Google Interview 21 The Apple Interview 22 The Facebook Interview 23 The Yahoo! Interview 24 Special Situations 25 Experienced Candidates 26 Testers and SDETs 27 Program and Product Managers 28 Dev Leads and Managers 30 Start-Ups 31 Before the Interview 33 Getting the Right Experience 34 Building a Network 35 Writing a Great Resume 37 Behavioral Questions 39 Behavioral Preparation 40 Handling Behavioral Questions 43 Technical Questions 45 Cracking the Coding Interview Table of Contents Technical Preparation 46 Handling Technical Questions 49 Five Algorithm Approaches 52 What Good Coding Looks Like 56 VII The Offer and Beyond 61 Handling Offers and Rejection 62 Evaluating the Offer 63 Negotiation 65 On the Job 66 VIII Interview Questions 67 Data Structures 69 Chapter | Arrays and Strings 71 Chapter | Linked Lists 75 Chapter | Stacks and Queues 79 Chapter 41 Trees and Graphs 83 Concepts and Algorithms 87 Chapter | Bit Manipulation 89 Chapter | Brain Teasers 93 Chapter | Mathematics and Probability 97 Chapters | Object-Oriented Design 103 Chapter 91 Recursion and Dynamic Programming 107 Chapter 10 | Scalability and Memory Limits Ill Chapter 11 | Sorting and Searching 117 Chapter 12 | Testing 123 Knowledge Based 131 Chapter 13 | C and C++ 133 Chapter 14 | Java 141 Chapter 15 | Databases 147 Chapter 16 Threads and Locks 153 Additional Review Problems 161 Chapter 17 | Moderate 163 Chapter 18 Hard 167 CrackingTheCodinglnterview.com VII Table of Contents IX Solutions 169 Data Structures 171 Chapter | Arrays and Strings 171 Chapter | Linked Lists 183 Chapter Stacks and Queues 201 Chapter 41 Trees and Graphs 219 Concepts and Algorithms 241 Chapter | Bit Manipulation 241 Chapter | Brain Teasers 257 Chapter? | Mathematics and Probability 263 Chapter 81 Object-Oriented Design 279 Chapter | Recursion and Dynamic Programming 315 Chapter 10 | Scalability and Memory Limits 341 Chapter 11 Sorting and Searching 359 Chapter 12 | Testing 377 Knowledge Based 385 Chapter 13 | C and C++ 385 Chapter 14 | Java 399 Chapter 15 | Databases 407 Chapter 16 | Threads and Locks 415 Additional Review Problems 429 Chapter 17 | Moderate 429 Chapter 18 | Hard 461 X Acknowledgements 491 XI Index 492 XII About the Author Join us at www.CrackingTheCodinglnterview.com to download full, compilable Java / Eclipse solutions, discuss problems from this book with other readers, report issues, view this book's errata, post your resume, and seek additional advice VIII Cracking the Coding Interview 500 Foreword Dear Reader, Let's get the introductions out of the way I am not a recruiter I am a software engineer And as such, I know what it's like to be asked to whip up brilliant algorithms on the spot, and then write flawless code on a whiteboard I know because I've been asked to the same thing—in interviews at Google, Microsoft, Apple, and Amazon, among other companies I also know because I've been on the other side of the table, asking candidates to this I've combed through stacks of resumes to find the engineers who I thought might be able to actually pass these interviews And I've debated in Google's Hiring Committee whether or not a candidate did well enough to merit an offer I understand and have experienced the full hiring circle And you, reader, are probably preparing for an interview, perhaps tomorrow, next week, or next year You likely have or are working towards a Computer Science or related degree I am not here to re-teach you the basics of what a binary search tree is, or how to traverse a linked list You already know such things, and if not, there are plenty of other resources to learn them I am here to help you take your understanding of Computer Science fundamentals to the next level, to learn how to apply those fundamentals to crack the coding interview The 5th edition of Cracking the Coding Interview updates the 4th edition with over 200 pages of additional questions, revised solutions, new chapter introductions, and other content Be sure to check out our website, www.careercup.com, to connect with other candidates and discover new resources I'm excited for you and for the skills you are going to develop.Thorough preparation will give you a wide range of technical and communication skills It will be well-worth it no matter where the effort takes you! I encourage you to read these introductory chapters carefully They contain important insight that just might make the difference between a "hire"and a "no hire." And remember—interviews are hard! In my years of interviewing at Google, I saw some interviewers ask"easy"questions while others ask harder questions But you know what? Getting the easy questions doesn't make it any easier to get the offer Receiving an offer is not about solving questions flawlessly (very few candidates do!), but rather, it is about answering questions better than other candidates So don't stress out when you get a tricky question—everyone else probably thought it was hard too Study hard, practice, and good luck! Gayle L McDowell Founder / CEO, CareerCup.com Author of The Google Resume and Cracking the Coding Interview CrackingTheCodinglnterview.com Solutions to Chapter 18 | Hard possible rectangle area (which is maxWordLength2) and tries to build a rectangle of that size If it fails, it subtracts one from the area and attempts this new, smaller size The first rectangle that can be successfully built is guaranteed to be the biggest Rectangle maxRectangleQ { int maxSize = maxWordLength * maxWordLength; for (int z = maxSize; z > 0; z - - ) { // start -from biggest area for (int i = l; i ... Science fundamentals to the next level, to learn how to apply those fundamentals to crack the coding interview The 5th edition of Cracking the Coding Interview updates the 4th edition with over 200... Author of The Google Resume and Cracking the Coding Interview CrackingTheCodinglnterview.com Introduction Something's Wrong We walked out of the hiring meeting frustrated, again Of the ten "passable"... since they were in school either To crack the coding interview, you need to prepare with real interview questions You must practice on real problems and learn their patterns Cracking the Coding