LeetCode 347: Top K Frequent Elements

Let’s solve LeetCode problem 347: Top K Frequent Elements. The instructions are as follows: Given an integer array nums and an integer \(k\), return the \(k\) most frequent elements. You may return the answer in any order. Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 \(k\) is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. I solved this problem on NeetCode as well. The goal was to find a solution in \(O(n)\) time and space, where \(n\) is the length of nums. I’ll be showing this solution here. Even though some solutions that take \(O(n\log n)\) time are faster for LeetCode leaderboards, this solution will scale a bit better. Let’s dive in! ...

January 2, 2026 · 5 min · David Nabergoj

FCPP 6: Chuck-a-Luck

This is the solution to problem 6 from Fifty Challenging Problems in Probability by Frederick Mosteller (1987). The problem is paraphrased below; for reference, it is inspired by the original book. We’re playing a gambling game called Chuck-a-Luck at a carneval. We bet either 1, 2, 3, 4, 5, or 6 dollars. Three dice are rolled. If the bet appears on one, two, or three dice, we receive one, two, or three times our bet plus our money back. Otherwise, we lose our bet. ...

January 2, 2026 · 2 min · David Nabergoj

LeetCode 61: Rotate List

Let’s solve LeetCode problem 61: Rotate List. The instructions are as follows: Given the head of a linked list, rotate the list to the right by \(k\) places. Constraints: The number of nodes in the list is in the range [0, 500] -100 <= Node.val <= 100 0 <= k <= 2 * 10^9 Let’s dive in! Recalling array rotations In a recent post, I solved this exact problem for arrays (LeetCode problem 189). The method was a triple-reversal procedure: ...

January 2, 2026 · 5 min · David Nabergoj

FCPP 5: Coin in Square

This is the solution to problem 5 from Fifty Challenging Problems in Probability by Frederick Mosteller (1987). The problem is paraphrased below; for reference, it is inspired by the original book. We’re playing a game at a carneval. There is table with horizontal and vertical lines that create a grid of squares. The side of each square is one inch long. We throw a penny onto the table. If the penny doesn’t cross any line, we win a prize! The diameter of a penny is \(3/4\) inches. ...

January 1, 2026 · 2 min · David Nabergoj

FCPP 3: The Flippant Juror

This is the solution to problem 3 from Fifty Challenging Problems in Probability by Frederick Mosteller (1987). The problem is paraphrased below; for reference, it is inspired by the original book. Jury A consists of three jurors. The first two have a probability \(p\) of being correct. The third one chooses based on a fair coin toss. All three jurors vote independently. Jury B consists of a single juror that is correct with probability \(p\). ...

January 1, 2026 · 3 min · David Nabergoj