11. gayle in/gaylemcdgayle 11
z
Gayle Laakmann McDowell
What
is NOT
expected
To know the answers
To solve immediately
To code perfectly
(It’snice.Itjustdoesn’t
happen*.)
*Okayfine.Ithappenedonce,in2000+hiringpackets.
12. gayle in/gaylemcdgayle 12
z
Gayle Laakmann McDowell
What
IS
expected
Be excitedabout hard problems
More thanjust “correct”
Drive!
Keeptrying when stuck
Write real code
Showmehowyouthink!
23. Gayle Laakmann McDowell 23gayle in/gaylemcdgayle
Techniques to Develop Algorithms
Optimize
A. BUD
B. Space/time
C. Doityourself
Solve
D. Recursion
E. Solve “incorrectly”
F. Other data structures
Pushyourself!
24. Gayle Laakmann McDowell 24gayle in/gaylemcdgayle
(A) Look for BUD
Bottlenecks
Unnecessary work
Duplicated work
37. Gayle Laakmann McDowell 37gayle in/gaylemcdgayle
(C)Do it yourself
Findpermutationsof swithinb
s = abbc
b = babcabbacaabcbabcacbb
Findthem!
… now how didyou actuallydoit?
38. Gayle Laakmann McDowell 38gayle in/gaylemcdgayle
Techniques to Develop Algorithms
Optimize
A. BUD
B. Space/time
C. Doityourself
Solve
D. Recursion
E. Solve “incorrectly”
F. Other data structures
Pushyourself!
39. Gayle Laakmann McDowell 39gayle 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
40. Gayle Laakmann McDowell 40gayle in/gaylemcdgayle
(E) Solve “incorrectly”
① Develop incorrectsolution
② Identifywhy preciselyit’s incorrect
③ Repair
④ (& Repeat)
41. Gayle Laakmann McDowell 41gayle in/gaylemcdgayle
(E)Solve “incorrectly”
Random node in BST
Try: flipcoin
Coin=Heads
Branch Left
Coin=Tails
Branch Right
42. Gayle Laakmann McDowell 42gayle 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
43. Gayle Laakmann McDowell 43gayle in/gaylemcdgayle
(E)Solve “incorrectly”
Random node in BST
Try:
Return rootwith1/n probability
Then flipcoin(heads left,tails->right)
44. Gayle Laakmann McDowell 44gayle in/gaylemcdgayle
(E)Solve “incorrectly”
Random node in BST
Try: pick random # 0 throughn-1
R=0
Return root
R>left.size
Branch right
1<=R<=left.size
Branch left