Commit graph

1558 commits

Author SHA1 Message Date
William Carroll
1783f371e9 Finish Tree section of LC problems
Wahoo! I need to remember that the inorder traversal of a BST should be
sorted. This piece of trivia comes in handy for a variety of BST related
problems.

I also think being able to do a {pre,in,post}-order traversal recursively and
iteratively is a skill that I need to develop.
2020-12-25 03:56:44 +00:00
William Carroll
93d7b5d8ea Solve a few String questions
Valid Anagram

This one is a classic: `sorted(a) == sorted(b)`

Group Anagrams

Using product of prime numbers to create a key for anagrams is much faster than
sorting the characters in each word. It is also satisfyingly simple.

Encode and Decode Strings

My initial implementation was clumsy and prone to fail for edge-cases. A more
elegant solution is using something like:

```python
def encode(words):
  return "".join("{}:{}".format(len(x), x) for x in words)
```
2020-12-25 03:52:54 +00:00
William Carroll
c34a3e2e97 Tread lightly into the Dynamic Programming section
After solving this, I was immediately stumped by the other DP questions, so I'm
taking a break.
2020-12-25 03:52:13 +00:00
William Carroll
4732a7456b Solve Binary "Sum of Two Integers"
This is tricky because Python has variable-width integers, so relying on two's
complement to support the sum of negative numbers results in infinite
recursion. I know three ways to combat this:
  1. Use Java.
  2. Conditionally branch and handle either addition or subtraction accordingly.
  3. Use a mask to enforce fixed-width integers in Python.
2020-12-25 03:50:18 +00:00
William Carroll
c389b46ecf Solve additional Tree problems
Only three more to go!
2020-12-22 18:22:40 +00:00
William Carroll
23b5dd754e Solve additional Matrix problems
Looks like "Rotate Image" is the only Matrix problem that remains. It was nice
to learn more about "Backtracking" -- a term I often encounter -- while
attempting to solve "Word Search".

From my current understanding, it is like Brute Force but with
short-circuiting. It also seems quite similar to Depth First Search, and I'm
currently unaware of how DFS and Backtracking differ. I'm hoping to learn more
though.
2020-12-22 03:03:11 +00:00
William Carroll
983b0fb276 Solve the Linked List questions
I did these during my flight from LON->NYC without wifi. I managed to get both
correct on the first attempt although I did not find the *optimal* solution for
"Reorder List". IMO "Reorder List" is the best Linked List question I've seen
because it covers a few essential Linked List tricks.
2020-12-18 19:55:18 +00:00
William Carroll
039e712656 Nest URLs beneath TODO entries
Tidying things up.
2020-12-18 14:41:26 +00:00
William Carroll
fd6029db69 Update remaining LC questions
Looks like I should prioritize the following topics:
- Dynamic Programming
- String
- Graph

Although I'm not sure how common DP questions are in interviews, DP is a useful
dragon to slay IMO.
2020-12-18 09:43:54 +00:00
William Carroll
9610ae5f5b Update Linked List LC questions
Snapshot my progress with Linked Lists...
2020-12-18 09:40:05 +00:00
William Carroll
88a22aba3d Update LC String questions
Looks like I have a few string questions to solve before closing that chapter.
2020-12-18 09:38:23 +00:00
William Carroll
262a0b45fb Mark LC Tree questions as done
Making sure that this document closely approximates the state of my LC
progress.
2020-12-18 09:36:28 +00:00
William Carroll
a30bc0f21d Create offline, org file from TeamBlind LC questions
TeamBlind.com hosts a curated list of DS&As questions from LeetCode.com that the
author claims appropriately samples the topics worth exploring. I'm creating an
offline list so that I can track my progress and work while I'm traveling.
2020-12-18 09:31:50 +00:00
William Carroll
1cff020d66 Show Morning and Evening habits by default
Now that I've deployed this, and I have an iPad running in kiosk mode, I
realized that I'd like to show my morning routine and my evening routine.
2020-12-13 15:14:19 +00:00
William Carroll
27585cd874 Update Sunday and Morning chains
Adapting to changes.
2020-12-13 15:13:18 +00:00
William Carroll
894615af52 Add -r to cp to copy the directory
This should be the last hold-out before deploying habit-screens! :)
2020-12-13 14:25:18 +00:00
William Carroll
f99b61305b Commit compiled file, output.css to habit-screens
As you can see, I was previously `.gitignore`-ing this file, but because my
`default.nix` attempts to `cp output.css`, I need that file available.
2020-12-13 14:16:14 +00:00
William Carroll
6b9eae2fa3 Productionize habit-screens
At some point I should document or write a script for how I package Elm projects
with Nix to be deployed on my website. For now, I'm modeling everything after my
previous success LearnPianoChords.
2020-12-13 13:33:49 +00:00
William Carroll
e3c72f3fd6 Add project-local .gitignore
Since the `default.nix` file is specific to my tooling, I'm ignoring it.
2020-12-12 02:59:49 +00:00
William Carroll
cc4f67c388 Add usage instructions to top-level README
Also delete redundant `README` from `server` directory.
2020-12-12 02:49:49 +00:00
William Carroll
8c5e4e77ed Expose functions at API layer
Creating a simple HTTP RESTful API for exposing our `Server.semiprime`
function. It supports some help messages, primitive parsing and error handling,
and singular vs. batch processing of arguments.

For more sophisticated parsing and error-checking, I prefer to use Haskell's
Servant library.
2020-12-12 02:43:40 +00:00
William Carroll
45877a8b9c Include cache hit/miss info in return type
This can be useful downstream for diagnostics.
2020-12-12 02:43:35 +00:00
William Carroll
1a404a58de Expand 10^5 in README
I think it's more readable this way.
2020-12-12 02:41:09 +00:00
William Carroll
686766929a Simple Math tests
Calling `assert` within the `Enum.map` makes the errors more usable.
2020-12-12 01:36:22 +00:00
William Carroll
ee96a818e1 Define Server.semiprime
- Clear the boilerplate that `mix` generated
- Consume `Math.factor` to test which inputs are semiprimes
- Cache all inputs that are semiprimes as soon as we discover that they are
- semiprimes

I considered a couple things related to the Cache:
- Could save space by storing all semiprime factors in a tree. This would make
  the lookups more expensive. Also because the tree's depth would never exceed
  two (because all semiprimes only have two factors), the tree would be quite
  broad, and we may not be saving enough space for the trade to be worthwhile. I
  might be wrong about that though.
- We could consider pre-computing all semiprimes when we start the app, but
  without running some tests firsts, I'm not sure whether or not it's worth the
  trouble.
2020-12-12 01:32:31 +00:00
William Carroll
ab73220280 Define Cache.{list,clear} to help debugging
Since I'm often using `iex` for interactive development, these functions are
useful.
2020-12-12 01:31:51 +00:00
William Carroll
714ec29743 Define Cache and convert app to OTP
Define a simple in-memory key-value store for our cache.

TL;DR:
- Define `Cache` as a simple state-keeping `Agent`
- Define `Sup`, which starts our Cache process
- Define `App`, which starts our Supervisor process
- Whitelist `App` in `mix.exs`, so that it starts after calling `iex -S mix`
2020-12-12 01:04:39 +00:00
William Carroll
c0a88ba7d5 Add moduledoc to Extras
9 out of 10 doctors agree that every module needs a doc. Ask your doctor if
moduledocs are right for you!
2020-12-11 22:48:12 +00:00
William Carroll
9e2fbfde8e Move the habit-screens project into //website
I'd like to eventually deploy this to wpcarro.dev. Coming soon!
2020-12-11 22:47:04 +00:00
William Carroll
3feb8ceb9a Delete //website/habits
Accommodating space for my habit-screens project.
2020-12-11 22:46:37 +00:00
William Carroll
90035da32e Delete //website/habitgarden
This is change #2 in a series of other larger changes...
2020-12-11 22:45:39 +00:00
William Carroll
381c344563 Delete //website/days-of-week-habits
This is one small change in a series of other, larger changes.
2020-12-11 22:45:14 +00:00
William Carroll
1eab926121 Define stubbed default.nix
In case I want to package this project with Nix. For now, it's useful to store
this at the project root because it help my Emacs's `project-find-file`
function.
2020-12-11 22:43:53 +00:00
William Carroll
f1e4582392 Define Math module
Support `Math.factor` and cover it with tests.
2020-12-11 22:43:26 +00:00
William Carroll
6af5e4b82e Define Extras module
I'll use as the host for utility functions needed to extend the stdlib.
2020-12-11 22:42:55 +00:00
William Carroll
6ff814a6d3 Init Elixir project
Starting fresh with...

```shell
mix new server
```
2020-12-11 22:42:16 +00:00
William Carroll
c23a12746c Initialize the Semiprimes Service
This is an exciting take-home assignment because I get to write a service in
Elixir!
2020-12-11 18:52:35 +00:00
William Carroll
aa788b128c Update groceries/list.org
Oh the times they are a-changin'
2020-12-07 20:11:32 +00:00
William Carroll
6c3792e881 Define another function to illustrate Reservoir Sampling
Documenting a few ideas about Reservoir Sampling while it's fresh on my mind.
2020-12-07 20:10:50 +00:00
William Carroll
c6f6d5f33b Fix typo
It's Splitwise... not Transferwise!
2020-12-07 20:10:33 +00:00
William Carroll
9549dbb266 Update BFS impls
I've subtly been implementing breadth-first traversals in graphs
incorrectly. The change is subtle, but updating `seen` needs to happen
immediately after queuing an item.

The results will remain the same, but the runtimes will differ dramatically. I
didn't notice this until I attempted to complete LeetCode's "count islands"
challenge, and LeetCode rejected my solution because it could not finish before
timing out. After looking at other candidates' solutions and comparing them to
mine, I couldn't see any difference... except for this subtle difference.

This SO answer provides a helpful explanation:
https://stackoverflow.com/questions/45623722/marking-node-as-visited-on-bfs-when-dequeuing

The take-away lesson here is to always call `seen.add(..)` immediately after
enqueuing.
2020-11-23 23:21:20 +00:00
William Carroll
c00eed469c Solve "cafe order checker" (again)
Perhaps my fifth iteration of solving this problem.
2020-11-21 16:31:53 +00:00
William Carroll
6ccdb06717 Solve "permutation palindrome" (again)
Python's `collections` library really shines for this problem.
2020-11-21 16:24:14 +00:00
William Carroll
60d7ea5b91 Implement a queue using two stacks
The space cost is O(n). The runtime cost of enqueue is O(1); the runtime cost of
dequeue is O(n). Using the "accounting method", the cost of an item in the
system is O(1). Here's why:

+------------+----------------------------+------+
| enqueue    | push onto lhs              | O(1) |
+------------+----------------------------+------+
| lhs -> rhs | pop off lhs; push onto rhs | O(1) |
+------------+----------------------------+------+
| dequeue    | pop off rhs                | O(1) |
+------------+----------------------------+------+
2020-11-21 16:15:43 +00:00
William Carroll
417d3b5fff Implement a bottom-up fibonacci
The bottom-up solution run in O(n) time instead of O(2^n) time, which the
recursive solution runs as:

```
def fib(n):
    return fib(n - 2) + fib(n - 1)
```

Remember that exponential algorithms are usually recursive algorithms with
multiple sibling calls to itself.
2020-11-21 14:48:12 +00:00
William Carroll
70e74a4027 Solve "linked-list-cycles"
Write a predicate for checking if a linked-list contains a cycle. For additional
practice, I also implemented a function that accepts a linked-list containing a
cycle and returns the first element of that cycle.
2020-11-21 14:47:18 +00:00
William Carroll
cbdac30643 Reimplement bst-checker
Practice makes perfect. See the previous commit for a more details about this
solution.
2020-11-21 14:21:05 +00:00
William Carroll
2b5bbb98ca Refactor existing bst-checker implementation
I believe the previous solution is invalid. This solution works and it should be
more time and space efficient.

Space-wise our stack grows proportionate to the depth of our tree, which for a
"balanced" BST should be log(n). Doing a BFT on a BST results in memory usage of
n because when we encounter the leaf nodes at the final level in the tree, they
will be 1/2 * n for a balanced BST.
2020-11-21 14:14:50 +00:00
William Carroll
1dc6695a47 Solve merge-sorted-arrays (again)
InterviewCake.com has a section on Facebook's interview, so I'm attempting to
solve all of the problems on there even if that means I'm resolving
problems. The more practice, the better. Right?

URL: interviewcake.com/facebook-interview-questions
2020-11-21 13:41:33 +00:00
William Carroll
0ccaa22032 Solve "find duplicate" using a graph
This problem is unusually difficult, but the solution is elegant.
2020-11-21 13:35:05 +00:00