r/adventofcode • • 11h ago

Help/Question [2026 Day 1] Huge inputs

Trying to think of algorithms that can solve this for huge inputs, or where many of the assumptions about the input do not hold.

By huge I mean the inputs are large enough that you don't even have enough memory to store counters for every number in the input, let along read the entire input file into memory.

It's like the definition of "Big Data":

* It's not when you can't fit the entire dataset into memory, it's when you can't even fit the index into memory

I like the think about problems like this with increasing levels of restrictions.

With a file with only ~2000 lines we don't have to think about complexity that much, but if the file contained 4,000,000,000 lines we could have the situation where we have three numbers that only appeared once, three numbers that only appeared 3 times, and then 1,999,999,994 numbers that appear twice.

Another assumption that some solutions make is that the frequency count of the numbers that appear in the top and bottom 3 must themselves be relatively unique as 4 numbers that all appear 4 times cannot all be in the top or bottom 3 in a deterministic manner.

To counter this it's trivial to craft an input that has a huge number of lines where even the frequency counts are unique. e.g. Consider the input formed of numbers 1 to n where every number x appears x times. Every item has a unique frequency count but the answer is still deterministic. (And it is similarly trivial to swap 1, 2, 3, n-2, n-1, n with 6 other numbers in the range so the answer isn't obvious up front.)

a) Let's pretend that memory is constrained such that we don't even have enough memory to store counters for all of the unique elements in the input, but we are free to read/write to intermediate local files and that we have enough space to store a bit more than another copy of the original input file.

The standard approach here is to split the input file into n smaller files, making sure that any occurrence of a number always appears in the same file. So for number x we write it to file x mod n every time we encounter it in the input. We choose n such that we can process n individual counters in memory in one go.

Once we've split the input into n sub files we process each one of those in turn in memory using the standard O(n) algorithm and keep track of the top 3 and bottom 3 item counts we've seen as we go.

b) Now let's imagine that we have no extra local storage, can we still do this?

Yes, but it's going to be a lot slower. Instead of splitting the input into n smaller files we just iterate over the main input file n times only processing lines that have x mod n = a as we loop a from 0 to n-1. This has the same effect as above but there's a lot of redundant reading involved as the vast majority of inputs read in each loop will be ignored.

c) Final restriction is what if the input is presented to us as a stream that we can't rewind or replay, and we can't write to any local storage, and we can't keep track of all of the possible values in memory. Possible?

I can't think of anything that guarantees getting the right answer without making some assumptions about the input.

8 Upvotes

22 comments sorted by

3

u/coriolinus 9h ago

Ultimately we want some kind of map counter. But if we are at the point where there's so much data that we can't even keep a map because there are too many keys, then it should be possible to design a custom counter which, once it grows big enough, starts evicting entries from the middle. We sacrifice accuracy with a algorithm, but it should hopefully be fine for cases with a normally randomized distribution.

1

u/fnordargle 7h ago

The problem with "evicting entries from the middle" is you can only do that at, or near the end of the file, by which time you've done 99.9999% of the work and not saved anything.

Consider this contrived example:

Imagine you had an input where the first 2 million lines contain two instances of the numbers 1 to 1,000,000.

We can be in this state and have just 6 lines left to process and still able to have a unique deterministic solution. All you need is the last 6 lines to have one more occurrence of 3 numbers already in the file (so they become the three most frequently occurring numbers) and then 3 numbers that have not appeared in the file before, and they become the least 3 frequent.

(And don't say you can rely on everything having the same count at this point, as it is easy to create other contrived inputs that don't have this property.)

3

u/TheThiefMaster 9h ago

You can do it with only enough memory for four numbers and counts - but it would need a crazy number of passes through the file, keeping track of the three highest known and doing a pass per single number to count it!

More reasonably, you could cap your storage at the first million numbers and their counts, pass through the file accumulating the count of the first million numbers, then discard all but the top three and repeat for the next million. The larger you can push the temp storage, the more numbers you can count per pass.

The actual implementations you've seen just assume you can have enough cache for a single pass count of all numbers.

Alternatively, you can sort the file, and then pass through it counting adjacent duplicates and keeping track of the 3 highest. This can be done reasonably linearly using a merge sort so wouldn't be too bad on the disk.

1

u/fnordargle 7h ago edited 1h ago

Doing it in batches and going over the file again and again is what I describe below section (b).

Sorting the input file requires huge resources and is computationally very expensive. If you want to try for yourself, and have the disk space, write 20GB of random integers to a file and then have something try and sort it. It takes much longer than you'd expect.

Also writing the file out to n different chunks and then processing each linearly (without sorting) will obviously be faster than writing the file out to n different chunks, sorting each of those, and then combining them (which is what merge sort effectively does) before processing the entire sorted file linearly.

9

u/XLNBot 11h ago

There are big data algorithms that make this more efficient, but they give up accuracy and they fail if you give them a specific input.

The only way to do this with 100% accuracy regardless of the input (that I know of) is storing a hashmap of n key - count pairs (where n is the number of distinct elements).

You can then pick the three most (or least) common elements with a single iteration over all the pairs

1

u/thisismyfavoritename 4h ago

cant you also just sort the array and then count the number of times the value is the same. Not saying its any better, probably even less so for "big data"

1

u/XLNBot 4h ago

You can also look at the biggest number in the array (let's call it M) and then you can just make an new array (let's call it A) of size M. You then iterate once over all numbers: for every number N in the array you can just do A[N]+=1.

Then you can just do one extra pass to find the 3 most/least common elements

1

u/fnordargle 1h ago

OK, you input consists of the following lines, how does this algorithm perform:

1
2
3
4
4
4
5
5
5
4294967296
4294967296

?

If in doubt, swap 4294967296 for 264 = 18446744073709551616.

1

u/XLNBot 1h ago

Extremely bad, but it's a solution that many people used for today's challenge (which has a purposefully low M). For a generic input, the hashmap is the best solution

1

u/fnordargle 1h ago

Indeed, and we've all been there writing solutions that are a bit too specific to the data we have infront of us. But then a lot of the time that is good enough.

What thinking like this does eventually bring you away from is the idea that there is one specific perfect/optimal solution. It's just not true. A lot of programming is about understanding the shape/size/scope of the data, not just focusing on the core algorithm and thinking that there's only one way to solve it for every possible input.

1

u/1234abcdcba4321 1h ago

Many posts about theoretical extensions to problems (such as this one!) which are about different inputs deliberately assume the input does not allow for easy simplifications that are available in the actual input, so "it works for the actual input" is not a good argument for a post like this.

1

u/XLNBot 4h ago

You can, that would use more memory but it would allow you to solve it with just one sort and one iteration

2

u/XLNBot 11h ago

This does not require you to store the whole dataset and it can be pretty efficient if you assume that n is low (which is an assumption that you usually can't make)

2

u/daggerdragon 3h ago edited 1h ago

Changed flair from Upping the Ante to Help/Question since you're asking a question and not actually upping any ante by providing your own proof-of-concepts or whatnot.


Psst: your inline code Markdown (the backticks) is being escaped and we're seeing the string literal ` on old.reddit. Fix, please? edit: πŸ‘

1

u/fnordargle 1h ago

Done hopefully, and thank you!

1

u/daggerdragon 1h ago

πŸ‘

4

u/DelightfulCodeWeasel 9h ago

I was trying to do some searching on a very similar theme earlier, to see if there were any algorithms that could find the most frequent element(s) with a single pass through the list and without storing data proportional to the range of numbers in the input, and I ended up finding this paper which compares the internal size of the memory for an algorithm versus the number of possible sequence prefixes as part of a proof.

My extremely naive understanding of the argument goes something like this:

Imagine I'm going to give you a list of 2^31+1 x 32-bit numbers. You don't know this up front, but every single number in the first 2^31 is unique, and the 2^31+1th number is a duplicate of one of the numbers in the first 2^31. Using a 256Mb bit array we can keep track of which numbers we've already seen, and then you finally pick up the repeat on that very last number.

If you use less than 256Mb* to keep track of the numbers you've seen, then it means that there must be two or more distinct sequences of those first 2^31 numbers which map to the same internal memory state of the algorithm. Since there are two distinct sequences that the algorithm can't tell apart, it can't give you a guarantee that it's picked up the single repeat correctly.

I've almost certainly butchered parts of the argument, and I'm sure there are going to be shortcuts to encoding sequences that have certain properties that mean you can do it in less, but I think the overall gist is still that you need memory that's at least proportional to (some factor/power/log of) the size of the alphabet and the input stream - you're not going to be able to find an algorithm that works in a small memory footprint which can deal with arbitrarily large sequences and alphabets.

[*] Or some number of bits that can encode 2^32 choose 2^31, unsure which would be smaller...

1

u/1234abcdcba4321 5h ago

I believe it's impossible to detect the most common element in a single pass of an input with O(1) working memory.

As far as I know you need the "passes through the input" * "available memory" to be proportional to the input size.

1

u/fnordargle 1h ago

And I'd agree, so it comes down to what can you do when you start to relax one of these restrictions.

Another way of phrasing this is what is the minimum needed to be able to solve the problem if we ignore execution time?

Assuming we are counting in terms of a generic integer (for the value, a corresponding counter, a utility variable or the final answer) I reckon it can't be done with fewer than 10 generic integer storage slots:

* 6 = 3 to hold the top three frequency counts and 3 for their corresponding numbers

* 1 to hold whether we are doing the most frequent or the least frequent

* 2 = 1 to hold the current value we are checking for in the file and another for its corresponding counter

* 1 to hold the next highest value we are need to look at, or some sentinel if there is no next highest value

Then, from there, what memory requirements are needed to make significant performance improvements.

For example, we ditch the "most frequent or least frequent flag" and for 6 more slots to count the least frequent entries as we loop through the values. That roughly halves the execution time with just a small increase to 15 generic storage slots.

1

u/AutoModerator 1h ago

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

0

u/T1lted4lif3 8h ago

I was literally thinking about this, what I feel like you can do is do a stream sketch, so for most common and most rare you don’t need to store everything in the middle but estimate what are at the edges,

Maybe could do the trick? Not a stream expert though

2

u/fnordargle 7h ago

Again, you don't know what is at the edges until you've processed the vast majority of it, by which time it's too big to fit into our finite limited memory that we have said is too small to do so..

Take the contrived example of an input consisting of 2 million lines containing two occurrences each of 1 million values.

If we take the idea that it is impossible to hold all of the unique values in memory then what information are you trying to throw away. If you can't hold all of the unique values then you have to throw some away. And what if it was one of those values you've thrown away that appears in one of the next lines to make it one of the most frequent.

Similarly, you can't guarantee to be able to compress the data by storing ranges of consecutive numbers as there's no guarantee that any of the numbers are adjacent at all.

Thinking about it it's pretty simple to prove that it is impossible to solve this for a non-rewindable/rnon-replayable stream with a finite memory constraint and no local storage.