65
Gayle L.McDowell | Founder / CEO gayle in/gaylemcd gayle Cracking the Facebook Coding Interview I <3 Facebook! Facebook

Cracking the Facebook Coding Interview

Embed Size (px)

Citation preview

Page 1: Cracking the Facebook Coding Interview

Gayle L.McDowell | Founder / CEO

gayle in/gaylemcdgayle

Cracking the FacebookCoding InterviewI <3 Facebook!

Facebook

Page 2: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 2

Hi! I’m Gayle Laakmann McDowell

Author Interview Coach Interview Consulting

<dev>

</dev>

(CS)

(MBA)

Page 3: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 3gayle in/gaylemcdgayle

Yes! Slides are online!

Gayle.com Click “Speaking” Ctrl-F for “Facebook”

Page 4: Cracking the Facebook Coding Interview

Behavioral Questions

The soft squishy stuff.

Page 5: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 5

The Pitch / Resume Walk-Through

Page 6: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 6gayle in/gaylemcdgayle

Your Past Work

1 – 2 Hard Projects Hard / cool You were central Technical depth

All Past Work Challenges,

architecture, tradeoffs, mistakes, successes, motivations

What did YOU do?

Page 7: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 7

z

Gayle Laakmann McDowell

What

about

YOU?

Be PASSIONATE

Be KNOWLEDGEABLE

Be a GOOD TEAMMATE

Page 8: Cracking the Facebook Coding Interview

Technical QuestionsWhy why why why why why meeeee02

Page 9: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 9

z

Gayle Laakmann McDowell

What

ReallyHappens

Page 10: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 10

z

Gayle Laakmann McDowell

What

ReallyHappens

Knowledge Questions + Coding

Design/Scalability + Coding

Algorithms/Problem Solving + Coding

Page 11: Cracking the Facebook Coding Interview

Knowledge Questions

How do indices work in SQL?

Page 12: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 12gayle in/gaylemcdgayle

Knowledge Questions

If you list it, know it

If you don’t know it, admit it Derive it if

possible.

Page 13: Cracking the Facebook Coding Interview

Design

How would you design TinyURL?

Page 14: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 14

z

Gayle Laakmann McDowell

How

ToApproach

WWYDAW

hatouldouotork

Page 15: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 15

z

Gayle Laakmann McDowell

How

ToApproach

SKI

R

copeey componentsdentify issuesepair

Page 16: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 16

z

Gayle Laakmann McDowell

How

ToApproach

① Scope the Problem Ask questions Make appropriate

assumptions

② Define Key Components

Can be somewhat naïve

③ Identify Issues Bottlenecks, tradeoffs

④ Repair & Redesign

Page 17: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 17gayle in/gaylemcdgayle

How to Act

DRIVE Lead the

process Be open about

issues

TEAMWORK Be open to

feedback Tweak as

necessary

Page 18: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 18

z

Gayle Laakmann McDowell

How

To

Prepare

Read about design of major companies Twitter, Facebook,

Quora, Google, etc. Think about WHY

they’re designed that way

Learn/review key concepts Task queues,

databases, sharding, etc.

Practice questions

Page 19: Cracking the Facebook Coding Interview

Algorithm Preparation

Problem solving, not puzzles!

Page 20: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 20gayle in/gaylemcdgayle

Why?

Strong CS fundamentals

Analytical skills

Make tradeoffs

Push through hard problems

Communication

How you think

Page 21: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 21

Essential Knowledge

Data Structures

Algorithms Concepts

ArrayLists Merge Sort Big O Time

Hash Tables Quick Sort Big O Space

Trees (+ Tries) Breadth-First Search

Recursion

Graphs Depth-First Search

Memoization / Dynamic Programming

Stacks / Queues Binary Search

Heaps

Page 22: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 22

Preparation

Implement DS/Algorithms MASTER Big O Practice with interview questions Code on paper/whiteboard Mock interviews

PUSH YOURSELF!

Page 23: Cracking the Facebook Coding Interview

Solving Algorithms

How to… attempt to solve a hard problem

Page 24: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 24

z

Gayle Laakmann McDowell

What

is NOTexpected

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.

Page 25: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 25

z

Gayle Laakmann McDowell

What

ISexpected

Be excited about hard problems

More than just “correct”

Drive! Keep trying when

stuck Write real code

Show me how you think!

Page 26: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 26

z

Gayle Laakmann McDowell

How

ToApproach

Cra

ckin

gTh

eC

odin

gIn

terv

iew

.com

“Reso

urc

es”

Page 27: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 27Gayle Laakmann McDowell

step

1Listen (for clues)

Page 28: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 28gayle in/gaylemcdgayle

What’s the clue?

Anagram server Ex: rates -> aster, stare, taser,

tears

Clue: why is it on a server?

Page 29: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 29Gayle Laakmann McDowell

step

2Draw an Example

Big Enough

General Purpose

+

Page 30: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 30

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

Page 31: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 31

Ex: Intersection of Two Sorted Arrays

Better:

[1, 12, 15, 19, 20, 21][2, 15, 17, 19, 21, 25,

27]

Big No special cases

Page 32: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 32Gayle Laakmann McDowell

step

3Brute Force / Naive

Stupid & terrible is okay!

Page 33: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 33Gayle Laakmann McDowell

step

4Optimize

Walk through

brute force

Look for optimizations

Page 34: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 34gayle in/gaylemcdgayle

Techniques to Develop Algorithms

OptimizeA. BUDB. Space/timeC. Do it yourself

SolveD. RecursionE. Solve

“incorrectly”F. Other data

structures

Push yourself!

Page 35: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 35gayle in/gaylemcdgayle

(A) Look for BUD

BottlenecksUnnecessary workDuplicated work

Page 36: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 36gayle in/gaylemcdgayle

What’s the bottleneck?

Ex: counting the intersection[1, 12, 15, 19, 20, 21][2, 15, 17, 19, 21, 25, 27]

Bottleneck: searching

B

Page 37: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 37gayle in/gaylemcdgayle

What’s unnecessary?

Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000

Unnecessary: looking for d U

Page 38: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 38gayle in/gaylemcdgayle

What’s unnecessary?

Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000

Unnecessary: looking for d U

Page 39: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 39gayle in/gaylemcdgayle

What’s duplicated?

Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000

Duplicated: c, d pairs D

Page 40: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 40gayle in/gaylemcdgayle

What’s duplicated?

Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000

Duplicated: c, d pairs D

Page 41: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 41gayle in/gaylemcdgayle

What’s duplicated?

Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000

D

Page 42: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 42gayle in/gaylemcdgayle

What’s duplicated?

Ex: a3 + b3 = c3 + d3 (1 <= a, b, c, d <= 1000

D

Page 43: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 43gayle in/gaylemcdgayle

(B) Space/Time Tradeoffs

Hash tables & other data structures

Precomputing

Page 44: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 44gayle in/gaylemcdgayle

Space/Time Tradeoffs Hash tables

Find pairs with sum[5, 15, 8, 9, 3, 2, -1, 4], sum = 7pairs: (5, 2), (8, -1), (3, 4)

Put items into hash table

Page 45: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 45gayle in/gaylemcdgayle

Space/Time Tradeoffs Precomputing

Find rectangle at origin w biggest sum

6 5 -9 2 -2 -5 -2 7 3 -2 10 13 -8 -3 1 -2 Brute force: compute all

rectangles and sums

Page 46: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 46gayle in/gaylemcdgayle

Space/Time Tradeoffs Precomputing

Find rectangle with biggest sum 6 5 -9 2 -2 -5 -2 7 3 -2 10 13 -8 -3 1 -2

-+ + 10

=

Page 47: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 47gayle in/gaylemcdgayle

Space/Time Tradeoffs Precomputing

Find rectangle with biggest sum 6 5 -9 2 -2 -5 -2 7 3 -2 10 13 -8 -3 1 -2

-+ + 13

=

Page 48: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 48gayle in/gaylemcdgayle

(C) Do it yourself

Find permutations of s within b s = abbc b = babcabbacaabcbabcacbb

Find them! … now how did you actually do it?

Page 49: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 49gayle in/gaylemcdgayle

Techniques to Develop Algorithms

OptimizeA. BUDB. Space/timeC. Do it yourself

SolveD. RecursionE. Solve

“incorrectly”F. Other data

structures

Push yourself!

Page 50: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 50gayle in/gaylemcdgayle

(D) Recursion / Base Case & Build

Subsets of a set {} {} {a} {}, {a} {a, b} {}, {a}, {b}, {a, b} {a, b, c} …

Subsets of {S1…Sn-1} + Sn to each

Page 51: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 51gayle in/gaylemcdgayle

(E) Solve “incorrectly”

① Develop incorrect solution② Identify why precisely it’s

incorrect③ Repair④ (& Repeat)

Page 52: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 52gayle in/gaylemcdgayle

(E) Solve “incorrectly”

Random node in BST

Try: flip coin

Coin = Heads Branch Left

Coin = Tails Branch Right

Page 53: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 53gayle in/gaylemcdgayle

(E) Solve “incorrectly”

Random node in BST

Try: random number in {0, 1, 2}

R = 0 Branch Left

R = 2 Branch Right

R = 1 Return root

Page 54: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 54gayle in/gaylemcdgayle

(E) Solve “incorrectly”

Random node in BST

Try: Return root with 1/n probability Then flip coin (heads left, tails ->

right)

Page 55: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 55gayle in/gaylemcdgayle

(E) Solve “incorrectly”

Random node in BST

Try: pick random # 0 through n-1

R = 0 Return root

R > left.size Branch right

1 <= R <= left.size Branch left

Page 56: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 56gayle in/gaylemcdgayle

(F) Other Data Structures

Giving out phone numbers “I want any available number” “I want this number”

Try: sorted array? Sorted linked list? Hash table? BST?

Page 57: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 57Gayle Laakmann McDowell

step

5Walk Through

Know the variablesand when they change

Page 58: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 58Gayle Laakmann McDowell

step

6Write Beautiful Code

Page 59: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 59gayle in/gaylemcdgayle

How to Write Whiteboard Code

Modularized Error cases /

TODOs Good

variables

Write straight Top-left corner Use arrows if

needed

Language choice is up to you!

Page 60: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 60

Modularization

Page 61: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 61Gayle 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

Page 62: Cracking the Facebook Coding Interview

Final Thoughts

And questions03

Page 63: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayle 63

z

Gayle Laakmann McDowell

It’s done

for areason!

Be a great teammate.

Be a great engineer.

Page 64: Cracking the Facebook Coding Interview

Gayle Laakmann McDowell 64gayle in/gaylemcdgayle

Yes! Slides are online!

Gayle.com Click “Speaking” Ctrl-F for “Facebook”

Page 65: Cracking the Facebook Coding Interview

gayle in/gaylemcdgayleGayle Laakmann McDowell 65

Other Resources

Gayle.com

CareerCup.com

CrackingTheCodingInterview.com

Or, follow me online • facebook.com/gayle• twitter.com/gayle• gayle.com• [email protected]• quora.com