GAYLE LAAKMANN CAREERCUP.COM CRACKING THE CODING INTERVIEW Now in the 4 th edition, Cracking the Coding Interview gives you the interview prepara- tion you need to get the top software developer jobs. This book provides: From binary trees to binary search, this list of 150 questions includes the most common and most useful questions in data structures, algorithms, and knowledge based questions. Using these ve approaches to solving algorithm questions, you can conquer tough interview questions and impress your interviewer. Don’t lose your dream job by making these common mistakes. Learn what many candidates do wrong, and how to avoid these issues. Stop meandering through an endless set of questions, while missing some of the most important preparation techniques. Follow these steps to more thoroughly prepare in less time. Humorous but instructive stories from our interviewers show you how some can- didates really opped on the most important question - and how you can avoid doing the same. founded CareerCup.com in 2005 to provide technical interview coaching for software engineers. As a Software Engineer at Microsoft, Apple and Google, she interviewed over one hundred and twenty applicants in three countries. She holds Bachelor’s and Master’s degrees in Computer Science from the University of Penn- sylvania. GAYLE LAAKMANN Founder and CEO, CareerCup.com 150 programming interview questions and solutions Plus: • Five proven approaches to solving tough algorithm questions • Ten mistakes candidates make and how to avoid them • Steps to prepare for behavioral and technical questions • Interviewer war stories: a view from the interviewer’s side FOURTH EDITION CRACKING THE C O D I N G I N T E R V I E W CRACKING THE CODING INTERVIEW CRACKING THE C O D I N G I N T E R V I E W 150 Programming Interview Questions and Solutions GAYLE LAAKMANN Founder and CEO, CareerCup.com CareerCup, LLC Seattle, WA CRACKING THE CODING INTERVIEW, FOURTH EDITION Copyright © 2008 - 2010 by Gayle Laakmann. All rights reserved. Published by CareerCup, LLC, Seattle, WA. Version 3.21090410302210. Visit our website at: www.careercup.com. No part of this book may be used or repro- duced in any manner without written permission except in the case of brief quota- tions in critical articles or reviews. For more information, contact support@careercup.com. Printed in United States of America 978-1-450-59320-5 9781450593205 (ISBN 13) Cracking the Coding Interview 1 4 5 7 The Microsoft Interview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 The Amazon Interview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 The Google Interview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The Apple Interview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 The Yahoo Interview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 13 20 Resume Advice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Behavioral Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Technical Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 28 Handling Behavioral Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 Handling Technical Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Five Algorithm Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 The Offer and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Top Ten Mistakes Candidates Make. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 44 46 Chapter 1 | Arrays and Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 2 | Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 Chapter 3 | Stacks and Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Chapter 4 | Trees and Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 56 Chapter 5 | Bit Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Chapter 6 | Brain Teasers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 2CareerCup.com Chapter 7 | Object Oriented Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Chapter 8 | Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Chapter 9 | Sorting and Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter 10 | Mathematical. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Chapter 11 | Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 12 | System Design and Memory Limits . . . . . . . . . . . . . . . . . . . . . . . . .71 74 Chapter 13 | C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter 14 | Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Chapter 15 | Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 Chapter 16 | Low Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Chapter 17 | Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Chapter 18 | Threads and Locks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 88 Chapter 19 | Moderate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 Chapter 20 | Hard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 94 301 303 304 4CareerCup.com Dear Readers, Welcome to the 4th edition of Cracking the Coding Interview. This volume updates the 3rd edition with new content and refreshed information. Be sure to check out our website, www. careercup.com, to connect with other candidates and to discover new resources. For those of you new to technical interviews, the process can seem overwhelming. Inter- viewers throw questions at you, expect you to whip up brilliant algorithms on the spot, and then ask you to write beautiful code on a whiteboard. Luckily, everyone else is in the same boat, and you’re already working hard to prepare. Good job! As you get ready for your interviews, consider these suggestions: » Most interviewers won’t give you a computer and will instead expect you to write code on a whiteboard or on paper. To simulate this environment, try answering interview problems by writing code on paper rst, and then typing them into a computer as-is. Whiteboard / paper coding is a special skill, which can be mastered with constant practice. » While technical skills are extremely important, that’s no reason to neglect your own resume. Make sure to prepare yourself to give a quick summary of any project or job you were involved with, and to discuss the hardest and most interesting problems you encountered along the day. » While this book oers a representative sample of interview questions, there are still thousands of interview questions out there. Memorizing solu- tions is not a great use of your time. Rather, use this book to explore approaches to problems, to learn new concepts, and to practice your skills. » Interviewers want to understand how you think and approach prob- lems, so talk out loud while you’re solving problems. Let the interviewer see how you’re tackling the problem, and they just might guide you as well. 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 oer. Receiving an oer is not about solving questions awlessly (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! 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 eort takes you! Study hard, practice, and good luck! Gayle Laakmann [...]... 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 Interview is the result of my first-hand experience interviewing at top companies It is the result of hundreds of conversations with candidates It is the result of the thousands of candidate- and interviewer- contributed questions And it’s the result... to dust beneath the heels of their boots Right? Just like pledge week, back in freshman year? Right? Hmmm? Nothing could be further from the truth The team of developers and managers interviewing you have their own tasks and projects waiting for them, back at their own desks Believe me, they’re hoping that every interview is going to be the last one They'd rather be doing anything else There might be... unusual in that it often gives a decision (if you’re hired) on the same day Your interviewers will discuss your performance while you meet with a final interviewer CareerCup.com 12 Interview War Stories The View from the Other Side of the Front, by Peter Bailey For the eager candidate getting ready for a big job interview, Cracking the Coding Interview is an invaluable reference, containing excellent coaching... result of seeing so many interview questions from so many firms Enclosed in this book are 150 of the best interview questions, selected from thousands of potential problems 5 Cracking the Coding Interview Introduction My Approach The focus of Cracking the Coding Interview is algorithm, coding and design questions Why? Because while you can and will be asked behavioral questions, the answers will be as... person? While they may not literally be asking themselves “Would I have a beer with this guy (or gal)”, they are looking to see how well you would fit in with the team, and how you would affect team chemistry If they hire you, you’re all going to be spending a lot of time together for 13 Cracking the Coding Interview Interview War Stories the next few months or years, and they want to know that they can... “So, are there any hot chicks in our department?” Those are the interviews where we politely thank the people for their time, shake their hand (except for the sweaty guy), then turn to each other after the door closes and ask – did that really just happen? Nobody is saying that you have to be a bland, boring robot in a Brooks Brothers suit and tie Remember, the interview team wants you to be the one”,... months?” From the interviewer’s point of view, they’re picking a neighbor that they’re going to live and work with 200 hours per month for foreseeable future Would you really want a neighbor that smelled like a hog rendering plant? 19 Cracking the Coding Interview Before the Interview Before the Interview | Resume Advice What Resume Screeners Look For Resume screeners look for the same things that interviewers... up on these questions What’s Different: Your interviewers do not make the hiring decision Rather, they enter feedback which is passed to a hiring committee The hiring committee recommends a decision which can be—though rarely is—rejected by Google executives CareerCup.com 10 Behind the Scenes | The Apple Interview Much like the company itself, Apple’s interview process has minimal beaucracy The interviewers... feedback unless there’s something they want subsequent interviewers to drill into Towards the end of the day, your interviewers will compare notes and if everyone still feels you’re a viable candidate, you'll interview with the director and then VP of the organization you’re applying to While this decision is rather informal, it’s a very good sign if you make it This decision also happens behind the scenes... torture) »» The girl who wore an iPod through her interview, with the volume turned loud enough that she actually had to ask the interviewers to repeat themselves a few times »» The poor, hyper-nervous fellow who was sweating like a marathon runner for half an hour »» The girl who wore a T-shirt with an obscene political slogan to her interview »» The guy who asked (seriously) at the end of his interview, . GAYLE LAAKMANN CAREERCUP.COM CRACKING THE CODING INTERVIEW Now in the 4 th edition, Cracking the Coding Interview gives you the interview prepara- tion you need to get the top software developer. technical questions • Interviewer war stories: a view from the interviewer’s side FOURTH EDITION CRACKING THE C O D I N G I N T E R V I E W CRACKING THE CODING INTERVIEW CRACKING THE C O D I N G I. 3 04 4CareerCup.com Dear Readers, Welcome to the 4th edition of Cracking the Coding Interview. This volume updates the 3rd edition with new content and refreshed