K
Keith Schwarz
keithschwarz.com · Personal blog · on HN since 2011
Keith Schwarz, a Stanford computer science lecturer, writes about algorithms and data structures and keeps an archive of interesting code.
What HN saysHN commenters call his Archive of Interesting Code an amazing collection of algorithms and link his article on sampling discrete distributions, "Darts, Dice, and Coins".
50HN people shared it
25linked it in comments
28stories submitted
685story points
5 months agolast shared
2011people per year2026
Most-shared posts
- 1Darts, Dice, and Coins: A beautiful article on a beautiful algorithm19 people · 251 points · first shared 2011 · 13 comments on HN
- 2The Archive of Interesting Code14 people · 185 points · first shared 2011 · 24 comments on HN
- 3Smoothsort Demystified (2011)5 people · 103 points · first shared 2011 · 3 comments on HN
- 4This problem (reportedly) took Don Knuth, 24 hours to solve4 people · 1 points · first shared 2015 · 1 comments on HN
- 5CS143: Compilers (2011)1 people · 143 points · first shared 2015 · 62 comments on HN
- 6Finding the most-significant bit in constant time, independent of word size1 people · 2 points · first shared 2018 · discussion on HN
- 7Code1 people · first shared 2013
- 8Cs1431 people · first shared 2019
- 9Coursereader.pdf1 people · first shared 2019
- 10Script1 people · first shared 2020
- 11Code1 people · first shared 2013
- 12Code1 people · first shared 2012
- 13Homepage1 people · first shared 2017
What HN people wrote when they linked it
stablemap · 2017-10-16
It looks like this was written by Keith Schwarz:
http://www.keithschwarz.com
namelezz · 2017-01-23
> Remember Google infamously rejecting Max Howell, the original Homebrew author
If Donald Knuth interviewed with Google, he would fail too because he would need 24 hours to solve a hard level leetcode question[1]. Unfortunately, Google loves throwing those kind of questions at interviewees and expect them to come up with solutions within 1 hour interview, lol.
[1] - http://keithschwarz.com/interesting/code/?dir=find-duplicate
RcouF1uZ4gsC · 2016-03-13
Also, take a look at http://www.keithschwarz.com/interesting/
It is an amazing archive of all kinds of neat algorithms and data structures.
gamegoblin · 2014-11-10
A minor grumble, but the filenames are not intuitive at all to me... It makes finding what I'm looking for a bit difficult.
alea.py
clea.py
dlea.py
flea.py
ilea.py
lea.py
olea.py
plea.py
tlea.py
Also if anyone is interested in floating point weights, look at the alias method. Good tutorial here: http://www.keithschwarz.com/darts-dice-coins/
EDIT: User rawnlq beat me to mentioning the alias method. I second the recommendation.
robrenaud · 2014-07-08
There is a great article on engineering a whole bunch of increasingly good solutions the discrete random sampling problem.
http://www.keithschwarz.com/darts-dice-coins/
And the corresponding HN discussion.
https://news.ycombinator.com/item?id=3401510
wfunction · 2013-12-24
Worth taking a look at: http://www.keithschwarz.com/interesting/
andreer · 2013-03-05
An excellent writeup on different techniques to do this: http://www.keithschwarz.com/darts-dice-coins/
jemfinch · 2012-02-27
That's the same guy that did the fantastic writeup of Dijkstra's Smoothsort: http://www.keithschwarz.com/smoothsort/
erganemic · 2022-08-11
Coming up with algorithms on the spot isn't a viable strategy for technical interviews any more, even if you're a super-genius. Here's a question that the page author claims took Donald Knuth 24 hours to solve [1], which is classified on Leetcode as a Medium(!) difficulty question [2]. Leetcode Medium questions are typically the cut-off point for most FAANG-tier interview questions--and while this is a hard medium, I know someone who got a variation of it at a Meta interview, and was expected to solve it in 30 minutes. So unless you're 48 times faster at inferring algorithms than one of the best algorists in the world, memorizing tricks is the game you have to play if you want a FAANG-adjacent job!
Of course, then the natural reaction is "if your hiring process would pass on Donald Knuth, your hiring process is broken," which is absolutely true, already known, and (apparently) deemed acceptable.
[1] https://keithschwarz.com/interesting/code/?dir=find-duplicate
[2] https://leetcode.com/problems/find-the-duplicate-number/
eru · 2025-09-22
It depends a bit on exactly what you mean.
But eg if you want to write a new hash table (with a new hash function), you'd want to do pretty similar-ish analysis to figure out whether it's a good idea.
I used some neat math in my time to justify much simpler algorithms than what we were using before. (But not hash table related.)
https://www.keithschwarz.com/darts-dice-coins/ is also a joy to read. (As mentioned in the discussion on https://news.ycombinator.com/item?id=8006336 )
Have a look at eg https://www.eecs.harvard.edu/~michaelm/postscripts/handbook2001.pdf for some more in this flavour.
dahart · 2022-07-22
The Suffix Array is surprisingly cool and useful https://en.wikipedia.org/wiki/Suffix_array
The Alias Table for sampling a discrete distribution in O(1) time is a very clever idea https://www.keithschwarz.com/darts-dice-coins/
Imnimo · 2021-06-18
Maybe everyone's using this problem as their first screen:
https://keithschwarz.com/interesting/code/?dir=find-duplicate
yonatano · 2020-10-28
Keith Schwarz maintains an "Archive of interesting code" which has lots of C++ implementations: https://www.keithschwarz.com/interesting/
yyhhsj0521 · 2019-06-30
This can be generalized to having any distribution, simulating a die:
http://www.keithschwarz.com/darts-dice-coins/
kobiguru · 2019-04-05
This is a bad idea. because one missing comma could screw up the program not mention which compilers they would likely run the program.
That being said if you are comfortable with basics you can learn a lot from this book
http://www.keithschwarz.com/coursereader.pdf
direengineer · 2017-10-16
Btw, the instructor of this course has a very interesting side project exploring different data structures
http://www.keithschwarz.com/interesting/
jor-el · 2016-03-14
I remember while learning data structures and implementing them, I landed on http://www.keithschwarz.com/interesting/. It was enlightenment for me. By browsing through these examples I learned about good programming practices. Most importantly how commenting your code can help you even when you return after few years to fix something in it.
lekanwang · 2015-09-17
A lecturer at Stanford CS (Keith Schwarz) also has a good collection of various implementations of interesting algorithms: http://www.keithschwarz.com/interesting/
rawnlq · 2014-11-10
I was hoping to see an implementation the alias method [1] for sampling from discrete distributions! Nonetheless very cool.
[1] http://en.wikipedia.org/wiki/Alias_method or http://www.keithschwarz.com/darts-dice-coins/
curiousDog · 2014-08-20
A few other good resources:
Bit hacks: https://graphics.stanford.edu/~seander/bithacks.html
Algorithm implementations:
http://www.keithschwarz.com/interesting/
stiff · 2013-05-13
Euclid's Algorithm is certainly Algorithms 101, his discussion of it - far from it:
http://www.keithschwarz.com/interesting/code/?dir=euclid
abcd_f · 2013-05-13
Some are interesting, but there's lots of mundane and generally boring stuff too, e.g. [1]
[1] http://www.keithschwarz.com/interesting/code/?dir=argmax
pwg · 2012-02-27
Some source - with a lot of comments - that claims to implement the algorithm:
http://www.keithschwarz.com/interesting/code/?dir=inplace-merge
bhrgunatha · 2019-09-13
University Of Washington
CSEP501: Compiler Construction
http://courses.cs.washington.edu/courses/csep501/
Keith Schwartz Stanford Compiler course CS143
http://www.keithschwarz.com/cs143/
Stanford's Engineering Compiler course on Lagunita
https://lagunita.stanford.edu/courses/Engineering/Compilers/Fall2014/a…
Matt Might's courses are self educational blueprints
http://matt.might.net/teaching/compilers/
Book
Introduction to Compilers and Language Design
https://www3.nd.edu/~dthain/compilerbook/
I'm not a fan of uncurated link dumps, however:
Awesome compilers link aggregation on Github
https://github.com/aalhour/awesome-compilers
efreak · 2020-07-25
There's been a few flash runtimes in javascript and other languages. Most of them have been long abandoned.
https://github.com/tobeytailor/gordon
https://www.gnu.org/software/gnash/
In development:
https://github.com/lightspark/lightspark
https://github.com/ruffle-rs/ruffle
And then the most recent one, CheerpX, which as I understand it runs the Adobe flash runtime itself via wasm: https://leaningtech.com/pages/cheerpxflash.html
Which brings me to Java. Java left the browser even before flash did. Not all of these are javascript jvms, but all of them are intended to run Java applications in the browser (some are transpilers, others require code changes):
https://leaningtech.com/pages/cheerpj.html
https://github.com/java2script/java2script (this links to some less-complete projects)
https://github.com/Jivings/jsJVM
https://github.com/nurv/BicaVM
https://github.com/st-js/st-js/
https://github.com/plasma-umass/doppio
https://github.com/jtulach/bck2brwsr
https://www.keithschwarz.com/+script/
https://github.com/robterrell/orto
https://github.com/jdstroy/JavaPoly
https://gitlab.com/neoexpert/jvm/-/blob/master/jsjvm/README.md