tvl-depot/scratch/deepmind/part_two/todo.org
William Carroll 8d36c6d00f Solve InterviewCake's compute nth Fibonacci
While the "Dynamic programming and recursion" section hosts this problem, the
optimal solution does not use recursion. Many cite the Fibonacci problem as a
quintessential dynamic programming question. I assume these people expect an
answer like:

```python
def fib(n):
  cache = {0: 0, 1: 1}
  def do_fib(n):
    if n in cache:
      return cache[n]
    else:
      cache[n - 1] = do_fib(n - 1)
      cache[n - 2] = do_fib(n - 2)
      return cache[n - 1] + cache[n - 2]
  return do_fib(n)
```

The cache turns the runtime of the classic Fibonacci solution...

```python
def fib(n):
  if n in {0, 1}:
    return n
  return fib(n - 1) + fib(n - 2)
```

... from O(2^n) to a O(n). But both the cache itself and the additional stacks
that the runtime allocates for each recursive call create an O(n) space
complexity.

InterviewCake wants the answer to be solved in O(n) time with O(1)
space. To achieve this, instead of solving fib(n) from the top-down, we solve it
from the bottom-up.

I found this problem to be satisfying to solve.
2020-03-30 14:14:02 +01:00

2.2 KiB

Array and string manipulation

DONE Merging Meeting Times

DONE Reverse String in Place

DONE Reverse Words

DONE Merge Sorted Arrays

DONE Cafe Order Checker

Hashing and hash tables

DONE Inflight Entertainment

DONE Permutation Palindrome

DONE Word Cloud Data

DONE Top Scores

Greedy Algorithms

DONE Apple Stocks

DONE Highest Product of 3

DONE Product of All Other Numbers

DONE Cafe Order Checker

DONE In-Place Shuffle

Sorting, searching, and logarithms

DONE Find Rotation Point

TODO Find Repeat, Space Edition

DONE Top Scores

DONE Merging Meeting Times

Trees and graphs

DONE Balanced Binary Tree

DONE Binary Search Tree Checker

DONE 2nd Largest Item in a Binary Search Tree

DONE Graph Coloring

DONE MeshMessage

DONE Find Repeat, Space Edition BEAST MODE

Dynamic programming and recursion

DONE Recursive String Permutations

DONE Compute nth Fibonacci Number

TODO Making Change

TODO The Cake Thief

DONE Balanced Binary Tree

DONE Binary Search Tree Checker

DONE 2nd Largest Item in a Binary Search Tree

Queues and stacks

TODO Largest Stack

TODO Implement A Queue With Two Stacks

TODO Parenthesis Matching

TODO Bracket Validator

Linked lists

DONE Delete Node

TODO Does This Linked List Have A Cycle?

TODO Reverse A Linked List

TODO Kth to Last Node in a Singly-Linked List

DONE Find Repeat, Space Edition BEAST MODE

System design

TODO URL Shortener

TODO MillionGazillion

TODO Find Duplicate Files

General programming

TODO Rectangular Love

TODO Temperature Tracker

Bit manipulation

TODO Binary Numbers

TODO The Stolen Breakfast Drone

Combinatorics, probability, and other math

TODO Which Appears Twice

TODO Find in Ordered Set

DONE In-Place Shuffle

TODO Simulate 5-sided die

TODO Simulate 7-sided die

TODO Two Egg Problem

JavaScript

TODO JavaScript Scope

TODO What's Wrong with This JavaScript?

Coding interview tips

TODO How The Coding Interview Works

TODO General Coding Interview Advice

TODO Impostor Syndrome

TODO Why You Hit Dead Ends

TODO Tips for Getting Unstuck

TODO The 24 Hours Before Your Interview

TODO Beating Behavioral Questions

TODO Managing Your Interview Timeline