Cracking the Facebook Coding Interview

Preview:

Citation preview

Gayle L.McDowell | Founder / CEO

gayle in/gaylemcdgayle

Cracking the FacebookCoding InterviewI <3 Facebook!

Facebook

gayle in/gaylemcdgayleGayle Laakmann McDowell 2

Hi! I’m Gayle Laakmann McDowell

Author Interview Coach Interview Consulting

<dev>

</dev>

(CS)

(MBA)

Gayle Laakmann McDowell 3gayle in/gaylemcdgayle

Yes! Slides are online!

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

Behavioral Questions

The soft squishy stuff.

gayle in/gaylemcdgayleGayle Laakmann McDowell 5

The Pitch / Resume Walk-Through

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?

gayle in/gaylemcdgayle 7

z

Gayle Laakmann McDowell

What

about

YOU?

Be PASSIONATE

Be KNOWLEDGEABLE

Be a GOOD TEAMMATE

Technical QuestionsWhy why why why why why meeeee02

gayle in/gaylemcdgayle 9

z

Gayle Laakmann McDowell

What

ReallyHappens

gayle in/gaylemcdgayle 10

z

Gayle Laakmann McDowell

What

ReallyHappens

Knowledge Questions + Coding

Design/Scalability + Coding

Algorithms/Problem Solving + Coding

Knowledge Questions

How do indices work in SQL?

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.

Design

How would you design TinyURL?

gayle in/gaylemcdgayle 14

z

Gayle Laakmann McDowell

How

ToApproach

WWYDAW

hatouldouotork

gayle in/gaylemcdgayle 15

z

Gayle Laakmann McDowell

How

ToApproach

SKI

R

copeey componentsdentify issuesepair

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

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

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

Algorithm Preparation

Problem solving, not puzzles!

Gayle Laakmann McDowell 20gayle in/gaylemcdgayle

Why?

Strong CS fundamentals

Analytical skills

Make tradeoffs

Push through hard problems

Communication

How you think

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

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!

Solving Algorithms

How to… attempt to solve a hard problem

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.

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!

gayle in/gaylemcdgayle 26

z

Gayle Laakmann McDowell

How

ToApproach

Cra

ckin

gTh

eC

odin

gIn

terv

iew

.com

“Reso

urc

es”

gayle in/gaylemcdgayle 27Gayle Laakmann McDowell

step

1Listen (for clues)

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?

gayle in/gaylemcdgayle 29Gayle Laakmann McDowell

step

2Draw an Example

Big Enough

General Purpose

+

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

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

gayle in/gaylemcdgayle 32Gayle Laakmann McDowell

step

3Brute Force / Naive

Stupid & terrible is okay!

gayle in/gaylemcdgayle 33Gayle Laakmann McDowell

step

4Optimize

Walk through

brute force

Look for optimizations

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!

Gayle Laakmann McDowell 35gayle in/gaylemcdgayle

(A) Look for BUD

BottlenecksUnnecessary workDuplicated work

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

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

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

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

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

Gayle Laakmann McDowell 41gayle in/gaylemcdgayle

What’s duplicated?

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

D

Gayle Laakmann McDowell 42gayle in/gaylemcdgayle

What’s duplicated?

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

D

Gayle Laakmann McDowell 43gayle in/gaylemcdgayle

(B) Space/Time Tradeoffs

Hash tables & other data structures

Precomputing

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

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

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

=

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

=

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?

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!

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

Gayle Laakmann McDowell 51gayle in/gaylemcdgayle

(E) Solve “incorrectly”

① Develop incorrect solution② Identify why precisely it’s

incorrect③ Repair④ (& Repeat)

Gayle Laakmann McDowell 52gayle in/gaylemcdgayle

(E) Solve “incorrectly”

Random node in BST

Try: flip coin

Coin = Heads Branch Left

Coin = Tails Branch Right

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

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)

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

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?

gayle in/gaylemcdgayle 57Gayle Laakmann McDowell

step

5Walk Through

Know the variablesand when they change

gayle in/gaylemcdgayle 58Gayle Laakmann McDowell

step

6Write Beautiful Code

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!

gayle in/gaylemcdgayleGayle Laakmann McDowell 60

Modularization

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

Final Thoughts

And questions03

gayle in/gaylemcdgayle 63

z

Gayle Laakmann McDowell

It’s done

for areason!

Be a great teammate.

Be a great engineer.

Gayle Laakmann McDowell 64gayle in/gaylemcdgayle

Yes! Slides are online!

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

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• gayle@gayle.com• quora.com

Recommended