programming interviews exposed - download.e-bookshelf.de · programming interviews exposed: ... or...

30

Upload: others

Post on 28-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 2: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 3: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

Programming interviews exPosed: secrets to Landing Your next Job

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

chaPter 1 Before the Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

chaPter 2 The Job Application Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

chaPter 3 Approaches to Programming Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . .19

chaPter 4 Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31

chaPter 5 Trees and Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61

chaPter 6 Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

chaPter 7 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

chaPter 8 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

chaPter 9 Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

chaPter 10 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

chaPter 11 Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

chaPter 12 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177

chaPter 13 Graphics and Bit Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

chaPter 14 Counting, Measuring, and Ordering Puzzles . . . . . . . . . . . . . . . . . . . . . 207

chaPter 15 Graphical and Spatial Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

chaPter 16 Knowledge-Based Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

chaPter 17 Nontechnical Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

aPPendix Résumés . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

concLusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

Page 4: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 5: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

Programming interviews exposed

third edition

Page 6: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 7: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

Programming interviews exposedSecretS to Landing Your next Job

third edition

John Mongan

Eric Giguère

Noah Kindler

Page 8: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

Programming Interviews Exposed: Secrets to Landing Your Next Job, Third Edition

Published byJohn Wiley & Sons, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.com

Copyright © 2013 by John Mongan, Eric Giguère, and Noah KindlerPublished by John Wiley & Sons, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-1-118-26136-1ISBN: 978-1-118-28720-0 (ebk)ISBN: 978-1-118-28340-0 (ebk)ISBN: 978-1-118-28466-7 (ebk)

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro-motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the pub-lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http:// booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.

Library of Congress Control Number: 2012941787

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other coun-tries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.

Page 9: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

For Thuy, the love of my life, who understands me.

—John Mongan

To my parents, Jean-Claude and Marie-Jolle, who

encouraged and supported my love of programming.

—Eric Giguère

To Mikey, Alex, and Teddy

—Noah Kindler

Page 10: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 11: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

about the authors

John mongan is a self-taught programmer with professional experience as a consultant for several software and pharmaceutical companies. He has three patents on software testing technologies. He holds a B.S. degree from Stanford and an M.D. and a Ph.D. degree in bioinformatics from UC San Diego, where he worked on supercomputer simulations of protein dynamics. He currently conducts research in medical informatics as a resident radiologist at UC San Francisco.

eric giguère started programming in BASIC on a Commodore VIC-20 (a long time ago) and was hooked. He holds BMath and MMath degrees in computer science from the University of Waterloo, has extensive professional programming experience, and is the author of several programming books. He currently works as a software engineer at Google.

noah KindLer is VP Technology at the security technology company Avira. He leads software design and development teams across several products with a user base of over 100 million.

Page 12: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 13: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

about the technicaL editors

michaeL giLbert is a long-time systems programmer for various engineering firms. He got his start developing games for the Atari ST, and was a frequent contributing editor for STart magazine. Over the years, he’s developed gaming software on the PC and Mac for clients worldwide. He’s also an expert Flash Actionscript programmer and has produced a popular internet gaming environment called HigherGames, you can check it out at www.highergames.com. He now enjoys developing games for the iPhone and iPad, and currently has four games in the AppStore (Woridgo, Jumpin’ Java, Kings Battlefield, and Set Pro HD). In his spare time, he enjoys trying to defeat his wife Janeen in a friendly game of Scrabble. You can follow him on Twitter at mija711.

Justin vogt is an experienced software development professional with a unique blend of skills (technical, architectural, design, communication, creative, management, and development leadership). He has over 15 years of diverse experience in software development and has worked on projects that include embedded software, mobile development, web development, commercial software develop-ment, device communications, medical application development, and non-profit organization solution development.

Page 14: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 15: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

executive editor Carol Long

ProJect editor Maureen Spears

technicaL editor Justin J . Vogt

Production editor Kathleen Wisor

coPY editor Apostrophe Editing

editoriaL manager Mary Beth Wakefield

freeLancer editoriaL manager Rosemarie Graham

associate director of marKeting David Mayhew

marKeting manager Ashley Zurcher

business manager Amy Knies

Production manager Tim Tate

vice President and executive grouP PubLisher Richard Swadley

vice President and executive PubLisher Neil Edde

associate PubLisher Jim Minatel

ProJect coordinator, cover Katie Crocker

comPositor Craig Johnson, Happenstance Type-O-Rama

Proofreader Nancy Carrasco

indexer Jack Lewis

cover designer Ryan Sneed

cover image © Andrew Rich / iStockPhoto

credits

Page 16: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 17: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

acKnowLedgments

the PreParation of this edition followed an unusual path, and we appreciate the extensive efforts of the staff at Wiley to bring it to a timely and successful completion. The contributions of our editor, Maureen Spears, who can rapidly overcome any obstacle that arises, and the personal attention of our publisher, Jim Minatel, and our senior acquisitions editor, Carol Long, were espe-cially key, and we thank them for their time, work, and assistance.

The quality of this edition has been greatly improved by Wayne Heym’s thoughtful comments and detailed review, and we thank him for his generous contributions.

In addition, John is deeply grateful for Michael J. Mongan’s help in facilitating his participation with this edition.

No third edition would have been possible without the two that preceded it, however, and the many people who contributed to them. For this reason, we also thank our original editors, Margaret Hendrey and Marjorie Spencer, for their patience and helpfulness. We are also grateful to our original reviewers and advisors, Dan Hill, Elise Lipkowitz, Charity Lu, Rob Maguire, and Tom Mongan. Dan’s contribu-tions in particular were tremendous — the quality of the first edition was vastly improved by his careful and meticulous reviews.

Page 18: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services
Page 19: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

contents

Preface xxv

IntroductIon xxix

chaPter 1: before the search 1

Know Yourself 1Know the Market 3

Basic Market Information 3

What About Outsourcing? 4

develop Marketable Skills 5get things done 6Manage Your online Profile 6Summary 8

chaPter 2: the Job aPPLication Process 9

Finding and contacting companies 9Finding Companies 9

Getting Referrals 10

Working with Headhunters 10

Contacting the Company Directly 11

Job Fairs 12

the interview Process 12Screening Interviews 12

On-Site Interviews 13

Dress 13

a recruiter’s role 14offers and negotiation 15

Dealing with Recruiter Pressures 15

Negotiating Your Salary 15

Accepting and Rejecting Offers 16

Summary 17

Page 20: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xviii

CONTENTS

chaPter 3: aPProaches to Programming ProbLems 19

the Process 19The Scenario 19

The Problems 20

Which Languages to Use 20

Interactivity Is Key 21

Solving the Problems 22The Basic Steps 22

When You Get Stuck 23

analyzing Your Solution 24Big-O Analysis In Action 25

How Big-O Analysis Works 26

Best, Average, and Worst Cases 27

Optimizations and Big-O Analysis 27

How to Do Big-O Analysis 28

Which Algorithm Is Better? 28

Memory Footprint Analysis 29

Summary 30

chaPter 4: LinKed Lists 31

Why Linked Lists? 31Kinds of Linked List 32

Singly Linked Lists 32

Doubly Linked Lists 34

Circular Linked Lists 34

basic Linked List operations 34Tracking the Head Element 34

Traversing a List 36

Inserting and Deleting Elements 36

Linked List Problems 38Stack Implementation 38

Maintain Linked List Tail Pointer 43

Bugs in removeHead 48

Mth-to-Last Element of a Linked List 50

List Flattening 53

List Unflattening 56

Null or Cycle 58

Summary 60

Page 21: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xix

CONTENTS

chaPter 5: trees and graPhs 61

trees 61Binary Trees 63

Binary Search Trees 64

Heaps 66

Common Searches 66

Traversals 67

graphs 68tree and graph Problems 68

Height of a Tree 69

Preorder Traversal 70

Preorder Traversal, No Recursion 71

Lowest Common Ancestor 73

Binary Tree to Heap 75

Unbalanced Binary Search Tree 77

Six Degrees of Kevin Bacon 79

Summary 83

chaPter 6: arraYs and strings 85

arrays 85C and C++ 86

Java 87

C# 87

JavaScript 88

Strings 88C 89

C++ 89

Java 90

C# 90

JavaScript 91

array and String Problems 91Find the First Nonrepeated Character 91

Remove Specified Characters 94

Reverse Words 97

Integer/String Conversions 101

Summary 105

Page 22: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xx

CONTENTS

Chapter 7: reCursion 107

Understanding Recursion 107Recursion Problems 111

Binary Search 111

Permutations of a String 113

Combinations of a String 116

Telephone Words 119

Summary 124

Chapter 8: sorting 125

Sorting Algorithms 125Selection Sort 126

Insertion Sort 127

Quicksort 128

Merge Sort 130

Sorting Problems 131The Best Sorting Algorithm 132

Stable Selection Sort 134

Multi-Key Sort 137

Make a Sort Stable 138

Optimized Quicksort 139

Pancake Sorting 142

Summary 144

Chapter 9: ConCurrenCy 145

Basic Thread Concepts 145Threads 145

System Threads versus User Threads 146

Monitors and Semaphores 146

Deadlocks 147

A Threading Example 147

Concurrency Problems 150Busy Waiting 150

Producer/Consumer 152

The Dining Philosophers 155

Summary 158

Page 23: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xxi

CONTENTS

chaPter 10: obJect-oriented Programming 159

Fundamentals 159Classes and Objects 159

Inheritance and Polymorphism 160

construction and destruction 162object-oriented Programming Problems 162

Interfaces and Abstract Classes 162

Virtual Methods 164

Multiple Inheritance 165

Summary 166

chaPter 11: design Patterns 167

What are design Patterns? 167Why Use Design Patterns? 167

Design Patterns in Interviews 168

common design Patterns 168Creational Patterns 168

Behavioral Patterns 171

Structural Patterns 172

design Pattern Problems 172Singleton Implementation 172

Decorator versus Inheritance 175

Efficient Observer Updates 176

Summary 176

chaPter 12: databases 177

database Fundamentals 177Relational Databases 177

SQL 178

Database Transactions 182

database Problems 183Simple SQL 183

Company and Employee Database 184

Max, No Aggregates 186

Three-Valued Logic 188

Summary 189

Page 24: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xxii

CONTENTS

Chapter 13: GraphiCs and Bit Manipulation 191

Graphics 191Bit Manipulation 192

Binary Two’s Complement Notation 192

Bitwise Operators 193

Optimizing with Shifts 194

Graphics Problems 194Eighth of a Circle 195

Rectangle Overlap 197

Bit Manipulation Problems 200Big-Endian or Little-Endian 201

Number of Ones 203

Summary 205

Chapter 14: CountinG, MeasurinG, and orderinG puzzles 207

Tackling Brainteasers 207Beware of Assumptions 208

Don’t Be Intimidated 209

Beware of Simple Problems 210

Estimation Problems 210

Brainteaser Problems 211Count Open Lockers 211

Three Switches 213

Bridge Crossing 214

Heavy Marble 218

Number of American Gas Stations 222

Summary 223

Chapter 15: GraphiCal and spatial puzzles 225

Draw It First 225Graphical and Spatial Problems 226

Boat and Dock 226

Counting Cubes 228

The Fox and the Duck 231

Burning Fuses 234

Escaping the Train 235

Summary 237

Page 25: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xxiii

CONTENTS

chaPter 16: KnowLedge-based Questions 239

Preparation 239Problems 240

C++ versus Java 241

Friend Classes 241

Argument Passing 242

Macros and Inline Functions 243

Inheritance 245

Garbage Collection 245

32-Bit versus 64-Bit Applications 247

Network Performance 247

Web Application Security 248

Cryptography 250

Hash Tables versus Binary Search Trees 251

Summary 251

chaPter 17: nontechnicaL Questions 253

Why nontechnical Questions? 253Questions 254

“What Do You Want to Do?” 254

“What Is Your Favorite Programming Language?” 255

“What Is Your Work Style?” 256

“What Can You Tell Me about Your Experience?” 256

“What Are Your Career Goals?” 256

“Why Are You Looking to Change Jobs?” 256

“What Salary Are You Expecting?” 257

“What Is Your Salary History?” 259

“Why Should We Hire You?” 260

“Why Do You Want to Work for This Company?” 260

“Do You Have Any Questions for Me?” 260

Summary 261

Page 26: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xxiv

CONTENTS

aPPendix: résumés 263

the technical résumé 263A Poor Example 263

Sell Yourself 267

Keep It Short 267

List the Right Information 268

Be Clear and Concise 269

Relevant Information Only 270

Use Reverse Chronological Ordering 271

Always Proofread 271

The Improved Example 271

Managers and Senior Developers 273

Tailor the Résumé to the Position 279

Sample résumé 279

concLusion 283

Index 285

Page 27: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

Preface

the most imPortant thing we have to teLL You is the same as in the first edition: You will get as much out of this book as you put into it. If you read this book cover to cover, you will learn something, but not nearly as much as you would if you take some time trying to work through the problems before you read the answers.

That said, many of the other things we have to tell you have changed over the period of more than a decade since the first edition was published, so it was thrilling to have another opportunity to revise Programming Interviews Exposed.

This edition represents the largest update yet. In addition to revising, expanding, and updating the material from the previous edition, chapters on the important topics of sorting and design patterns are added. The non-programming parts of the book were revised to reflect the realities of today’s job market. Throughout all this, we maintain the approachable style and step-by-step thought process developed for the original edition.

Code samples are largely in C, C++, or Java, but in most cases the focus is on the data structures and algorithms, and the language choice is entirely secondary. All the examples should be easily understandable for an experienced programmer.

One of us (Eric) recently interviewed for and landed his dream job at Google, which has given him additional perspective on programming interviews, reflected in this revision. We’re pleased that Google seems to be leading a shift away from the use of trivial puzzles in interviews, something we’ve complained about since the first edition (see the following original preface).

We hope you enjoy the third edition of Programming Interviews Exposed and that it helps you get the job you’ve always wanted. We’d love to hear your thoughts on the book and your interview expe-riences. You can contact us at [email protected]. Be sure to visit the official Programming Interviews Exposed site at http://www.piexposed.com for updates and more information.

Preface to the first edition

If you’re like us, you don’t usually read prefaces. This one has some useful information in it, though, so we hope you’ll make an exception. If you’re still tempted to skip the preface, here’s what you really need to know: You’ll get as much out of this book as you put into it. If you read this book cover to cover, you’ll learn something, but not nearly as much as you would if you take some time trying to work through the problems on your own before you read the answers.

This book will help prepare you for the interviews you will face when seeking a job in program-ming, development, technical consulting, or any other field that warrants a programming interview. Programming interviews bear little resemblance to those described in traditional job-hunting and interview books. They consist almost entirely of programming problems, puzzles, and technical

Page 28: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xxvi

Preface

questions about computers. This book discusses each of the kinds of problems you are likely to encounter and illustrates how they are best approached using questions from real interviews as examples.

At this point you may be wondering who we are and what gives us the authority to write this book. We’re both recent graduates who’ve been through a lot of interviews in the past few years. We’ve interviewed for jobs ranging from technical consulting with large established companies to writing device drivers for startups. This book is based on the experiences and observations we’ve taken from those interviews — what yielded offers and what didn’t. We believe that this is the best possible basis for a book like this. Rather than give you some HR exec’s idea of how interviewing should be done or a head hunter’s impression of how it might be done, we will tell you what interviews are really like at America’s top software and computer companies and what you need to do to get the job you want.

NOTE For the record, we don’t think that the way interviewing is done today is necessarily the way it should be done. The current paradigm puts too much emphasis on the ability to solve puzzles and familiarity with a relatively limited body of knowledge, and it generally fails to measure a lot of the skills that are critical to success in industry.

To that end, we haven’t made up any of the questions in this book. Every last one of them has been lifted from a recent interview. The distributions of problem type and difficulty are similar to what you should expect to encounter in your interviews. We must emphasize that the problems presented in this book are a representative sample of the questions asked in interviews, not a comprehensive compilation. Reading this book straight through and memorizing the answers would completely miss the point. You may be asked some of the questions that appear in this book, but you should not expect that. A large and constantly changing body of questions is asked, and any intelligent interviewer who has seen this book will never again use any of the questions that appear here. On the other hand, interview questions encompass relatively few topic areas and types of questions, and these rarely change. If you work on learning to solve not just the specific problems we present, but the types of problems we present, you’ll be able to handle anything they throw at you in an interview.

We’ve taken a couple of steps to facilitate the objective of improving your problem-solving skills. First, where appropriate, we provide reviews of important topics before we present questions on those topics. Second, instead of merely giving answers to the problems, we illustrate the problem-solving process from beginning to solution. We’ve found that most textbooks and nearly all puzzle books take a different approach to examples: They begin with a problem, go immediately to the answer, and then explain why the answer is correct. In our experience, the result is that the reader may under-stand the particular answer and why it’s right, but is left with no clue as to how the author came up with that solution or how a similar problem might be solved. We hope that our step-by-step approach to solutions will address this issue, helping you to understand not only the answers but also how you arrive at the answers.

Page 29: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services

xxvii

Preface

Learning by watching is never as effective as learning by doing. If you want to get the most out of this book, you will have to work out the problems yourself. We suggest the following method:

1 . After you read a problem, put the book down and try to work out the solution.

2 . If you get stuck, start reading the solution. We never blurt out the answer at the beginning, so you don’t have to worry that we’re going to give away the entire solution.

3 . Read just far enough to get the hint you need, and then put down the book and keep working.

4 . Repeat this as necessary.

The more of the solution you work out yourself, the better your understanding will be. In addition, this method closely resembles the actual interview experience, where you will have to solve the prob-lems yourself, but the interviewer will give you hints when you get stuck.

Programming is a difficult and technical art. It would be impossible to teach everything you need to know about computers and programming in one book. Therefore, we’ve had to make some assump-tions about who you are. We assume that you have a background in computers equivalent to at least the first year or two of a computer science degree. Specifically, we expect that you are comfortable with programming in C, that you’ve had some experience with object-oriented programming in C++ or perhaps Java, and that you know the fundamentals of computer architecture and computer science theory. These are effectively the minimum requirements for a general development job, so most interviewers will have similar expectations. If you find yourself lacking in any of these areas, you should seriously consider seeking more education before starting your job search and interviews.

It’s also possible that you have a great deal more computer knowledge and experience than what we’ve described as the minimum requirements. If so, you may be particularly interested in some of the more advanced topics included. However, don’t ignore the basic topics and questions, no mat-ter how much experience you have. Interviewers tend to start with the fundamentals regardless of what’s on your résumé.

We have made every effort to ensure that all of the information in this book is correct. All of the code has been compiled and tested. Nevertheless, as you probably know all too well from your own programs, a few bugs and errors are inevitable. As we become aware of such problems, we will post corrections at http://www.piexposed.com.

We’re confident that you’ll find this book useful in getting the job you want. We hope that you may also find it an entertaining exploration of some clever puzzles in your chosen profession. If you’d like to tell us about your reaction to our book, share your thoughts on any particular problem or topic, or provide a problem from one of your recent interviews, we’d love to hear from you. Please e-mail us at [email protected].

Go find a killer job!

Page 30: Programming interviews exPosed - download.e-bookshelf.de · Programming interviews exPosed: ... or other professional services. If professional assistance is required, the services