ctci/10. Sorting and searching/10.7. Missing int.md

741 B

10.7. Missing int

Given an input file with 4B non-negative integers, generate an integer not contained in the file. Assume you have 1GB memory available for this task. Follow up question: what if you only have 10MB of memory? Assume that all values are distinct and there are 1B non-negative integers

I would load 1GB of data into memory. Then, with Python, I would create a set of an index from 1 to 4B and obtain the difference with my index and the input file. Do the same for 4 chunks, the difference with each is the set with all numbers not present in the input file. This would be O(n) space complexity and O(n/available_memory) time complexity.

Solution

It involves bit vectors, indexing. Quite complicated to understand.