July 26th, 2024

Finding a random seed that solves a LeetCode problem (2023)

Marco Cognetta explores generating a unique bitstring not in a given list using a random seed and a deterministic hash function, successfully identifying a valid seed after 42 attempts.

Read original articleLink Icon
Finding a random seed that solves a LeetCode problem (2023)

Marco Cognetta discusses his approach to solving a LeetCode problem by finding a random seed that generates a unique bitstring not present in a given list of unique bitstrings. The challenge involves generating a new bitstring of the same length as the input strings, which can be approached through various methods, including Cantor's diagonalization argument. Cognetta's method involves using a random seed influenced by the input to ensure that the generated bitstring is not already in the list. He explains the probability of randomly generating a valid answer, noting that while the odds seem low, they improve with smaller input sizes.

He details his implementation, which includes creating a deterministic hash function to generate the seed based on the input bitstrings. This function computes a value from the characters and their positions in the array. After testing various seeds, he successfully identified a seed that passed all test cases after 42 attempts. He reflects on potential improvements, such as optimizing the hash function and addressing input duplicates, which could have expedited the process. Overall, Cognetta's exploration highlights the intersection of randomness and algorithmic problem-solving in coding challenges.

Link Icon 6 comments
By @porphyra - 9 months
Reminds me of this rock paper scissors bot that has a 59% win rate against other algorithms: https://rpscontest.com/entry/614005
By @Xcelerate - 9 months
Haha, this is pretty funny. I immediately thought of Cantor's diagonal argument when I saw the question, but it makes me wonder — how long would it have taken me to solve the problem if I hadn't previously read about Cantor's argument in the context of Turing machines?

Here's a variant: "Given a list of k LeetCode problems sourced from a bag of n unique tricks, generate a new LeetCode problem that utilizes a trick not found in the bag."

I'm being facetious of course, but actually now I have an idea that we could create a bipartite graph mapping tricks to LeetCode problems. From there, given a willingness to memorize n tricks, we can compute the optimal bag of tricks to commit to memory in order to maximize the number of LeetCode problems quickly solvable during an interview, weighted by the probability of each problem's appearance.

By @RomanPushkin - 9 months
The same can be applied to a stock market. I am a big fan of looking into historical data, and I was using WealthLab for quite a while.

One of the funniest things is when you find "strategy" that performs best over one year by making from 50 to 100 deals. But don't get fooled, it's just a random parameters, and when applied to the next year or years, you won't get these results, of course.

So you're getting reliable results only when you can reproduce your success (no matter what it is) consistently.

By @freediver - 9 months
Reminds me of LLMs, where the weights are the 'seed' that solves standard benchmark suite.
By @b20000 - 9 months
so when is the last time you used this in your code you write on a day to day basis?