Upload
gayle-mcdowell
View
121
Download
2
Embed Size (px)
Citation preview
Gayle L.McDowell | Founder / CEO
gayle in/gaylemcdgayle
Cracking theCoding Interview
CareerCup
gayle in/gaylemcdgayleGayle Laakmann McDowell 2
Hi! I’m Gayle Laakmann McDowell
Author Interview Coach Interview Consulting
<dev>
</dev>
(CS)
(MBA)
What We Look ForThings that make you think01
Gayle Laakmann McDowell 4gayle in/gaylemcdgayle
Why?
Analytical skills
How you think Make
tradeoffs
Push through hard problems
Communication Strong CS
fundamentals
gayle in/gaylemcdgayle 5
z
Gayle Laakmann McDowell
Whatis NOT
expected
To know the answers To solve immediately To code perfectly
(It’s nice. It just doesn’t
happen*.)
* Okay fine. It happened once, in 2000+ hiring packets.
gayle in/gaylemcdgayle 6
z
Gayle Laakmann McDowell
WhatIS
expected
Be excited about hard problems
Drive! Keep trying when stuck More than just “correct”
Pay attention to me! Write real code
Show me how you think!
Preparation02
gayle in/gaylemcdgayleGayle Laakmann McDowell 8
Essential KnowledgeData Structures
Algorithms Concepts
ArrayLists Merge Sort Big O Time
Hash Tables Quick Sort Big O Space
Trees (+ Tries) & Graphs
Breadth-First Search
Recursion
Linked Lists Depth-First Search
Memoization / Dynamic Programming
Stacks / Queues Binary Search
Heaps
gayle in/gaylemcdgayleGayle Laakmann McDowell 9
Preparation
MASTER Big O Implement DS/Algorithms Practice with interview questions Code on paper/whiteboard Mock interviews
PUSH YOURSELF!
7 StepsThings that make you think03
gayle in/gaylemcdgayle 11
z
Gayle Laakmann McDowell
HowTo
Approach
Crac
king
TheC
odin
gInt
ervi
ew.c
om
“R
esou
rces
”
gayle in/gaylemcdgayle 12Gayle Laakmann McDowell
step
1Listen (for clues)
gayle in/gaylemcdgayle 13Gayle Laakmann McDowell
step
2Draw an Example INTERSECTION SIZE:
Find # elements in common between two sorted, distinct arrays:
gayle in/gaylemcdgayleGayle Laakmann McDowell 14
Ex: Intersection of Two Sorted Arrays
Most people draw something like this:
[1, 12, 15, 19][2, 12, 13, 20]
Too small Too special-case-y • same size, one common element, same
index
gayle in/gaylemcdgayleGayle Laakmann McDowell 15
Ex: Intersection of Two Sorted Arrays
Better:[1, 12, 15, 19, 20, 21][2, 15, 17, 19, 21, 25,
27] Big No special cases
gayle in/gaylemcdgayle 16Gayle Laakmann McDowell
step
2Draw an Example
Big Enough
General Purpose
+
gayle in/gaylemcdgayle 17Gayle Laakmann McDowell
step
3Brute Force / Naive
Stupid & terrible is okay!
gayle in/gaylemcdgayle 18Gayle Laakmann McDowell
step
4Optimize
Walk through
brute force
Look for optimizations
Gayle Laakmann McDowell 19gayle in/gaylemcdgayle
Techniques to Develop Algorithms
BUD Space and Time Do It Yourself
Push yourself!
Gayle Laakmann McDowell 20gayle in/gaylemcdgayle
(A) Look for BUD
BottlenecksUnnecessary workDuplicated work
Gayle Laakmann McDowell 21gayle in/gaylemcdgayle
What’s the bottleneck?
Ex: counting the intersection[1, 12, 15, 19, 20, 21][2, 15, 17, 19, 21, 25, 27]
Brute Force: O(A * B) Bottleneck: O(B) to search
Binary Search Algorithm: O(A log B) Bottleneck: O(log B) to search
Sorted Merge: O(A + B) B
Gayle Laakmann McDowell 22gayle in/gaylemcdgayle
What’s unnecessary?
Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000
Unnecessary: looking for d U
Gayle Laakmann McDowell 23gayle in/gaylemcdgayle
What’s unnecessary?
Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000
Unnecessary: looking for d U
Gayle Laakmann McDowell 24gayle in/gaylemcdgayle
What’s duplicated?
Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000
Duplicated: c, d pairs D
Gayle Laakmann McDowell 25gayle in/gaylemcdgayle
What’s duplicated?
Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000
Duplicated: c, d pairs D
c d c3 + d3
… … …4 31 298554 32 328324 33 36001… … …5 59 2055045 60 2161255 61 227106… … …
Gayle Laakmann McDowell 26gayle in/gaylemcdgayle
What’s duplicated?
Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000
Duplicated: c, d pairs D
c3 + d3 (c, d)… …29855 (4, 31)32832 (4, 32), (18, 30)36001 (4, 33)… …205504 (5, 59)216125 (5, 60), (45, 50)227106 (5, 61)… …
Gayle Laakmann McDowell 27gayle in/gaylemcdgayle
What’s duplicated?
Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000
D
Gayle Laakmann McDowell 28gayle in/gaylemcdgayle
(B) Space/Time Tradeoffs
Hash tables & other data structures
Precomputing
Gayle Laakmann McDowell 29gayle in/gaylemcdgayle
(C) Do it yourself
Find permutations of s within b
gayle in/gaylemcdgayleGayle Laakmann McDowell 30
find abbcd inbabc dbaef dbbac bddf ae0 1 2 3 4 5 6 7 8 9 10 1
1 12 13 14 15 16 17 18 19
Gayle Laakmann McDowell 31gayle in/gaylemcdgayle
(C) Do it yourself
Find permutations of s within b s = abbcd b =
Find them! … now how did you actually do it?
babc dbaef dbbac bddf ae
Gayle Laakmann McDowell 32gayle in/gaylemcdgayle
Techniques to Develop Algorithms
BUD Space and Time Do It Yourself Push yourself!
gayle in/gaylemcdgayle 33Gayle Laakmann McDowell
step
5Walk Through
Know the variablesand when they change
gayle in/gaylemcdgayle 34Gayle Laakmann McDowell
step
6Write Beautiful Code
Gayle Laakmann McDowell 35gayle in/gaylemcdgayle
How to Write Whiteboard Code
Top-left corner Short Hand
Helps
Good style Modularize
(upfront!)
Gayle Laakmann McDowell 36gayle in/gaylemcdgayle
Short Hand Helps
Good:
Good Enough & Easier:
gayle in/gaylemcdgayleGayle Laakmann McDowell 37
Good Style
Appropriate & Consistent Spacing
Good Variable NamesBAD
gayle in/gaylemcdgayleGayle Laakmann McDowell 38
Modularization
gayle in/gaylemcdgayle 39Gayle Laakmann McDowell
step
7Testing
FIRST Analyze What’s it doing? Why? Anything that looks
weird? Error hot spots
THEN use test cases Small test cases Edge cases Bigger test cases
BUT… Test code, not algorithm Think as you test Think before you fix
gayle in/gaylemcdgayle 40
z
Gayle Laakmann McDowell
HowTo
Approach
Crac
king
TheC
odin
gInt
ervi
ew.c
om
“R
esou
rces
”
If you forget everything elsePlease remember this04
gayle in/gaylemcdgayleGayle Laakmann McDowell 42
Please please please please
Create example Big (tiny is useless) Generic (not a special case)
Walk through it Figure out the output for that one input (Don’t worry about algorithms /
implementation) DO NOT CODE UNTIL YOU’RE
READY 100% sure that you know what you’re
doing 100% sure that your interview wants you
to code
gayle in/gaylemcdgayleGayle Laakmann McDowell 43
Other Resources
Gayle.com
CareerCup.com
CrackingTheCodingInterview.com
Or, follow me online • facebook.com/gayle• twitter.com/gayle• gayle.com• [email protected]• quora.com