> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/interview_questions_4/missing-int.md).

# Missing Int

**10.7 Missing Int:** Given an input file with four billion non-negative integers, provide an algorithm to generate an integer that is not contained in the file. Assume you have 1 GB of memory available for this task. FOLLOW UP What if you have only 10MB of memory? Assume that all the values are distinct and we now have no more than one billion non-negative integers.

* Brainstorm:
* Assuming 4 byte (32 bit) unsigned integers that represent 2^32 possible things. \[0 - 2^32]
* 4 billion integers have to consume 128,000,000,000 bits or 16 Billion bytes which is roughly = 16x10^12/1x10^9 = 16 GB of memory. Memory space is insufficient to represent all numbers individually.
* Idea: Perform external merge sort (above). Build 16 hash tables, that each contain n/2 space (500MB) that each store the contents of the digits. I chose to do this as a preprocessing step. Then I would read in the hash tables one by one from disk, and iterate to find the first missing positive in O(N) time.
* Solution:
* Use a bit vector to that hashes the value of the unsigned integer into the bitset.
* 1 GB of memory can hold 2^30 bytes or 2^30 x 8 bits (8 billion) distinguishable things. Because we only have 4 billion elements, these can all hash to the bit vector that contains 4 billion boolean positions. Duplicates can be handled as well because we only need to flip the bit if it is true.
* Then, iterate through the array, and find the first missing index.
* This will take O(N) space, and O(N) time.

```cpp
template<size_t size>
unsigned long int getFirstMissingNumber(bitset<size> bits) {
for (int i = 0; i < bits.size(); i++) {
if (!bits[i]) return i;
}
}

int main() {
srand(time(nullptr));
unsigned long int integers = 4000000000;

// (pow(2, 30) * 8) - 1 = 1GB = 8 billion bits
// 4294967295 (2^32-1) = unsigned long
bitset<ULONG_MAX> bits;
bits.reset(); // set to zero

for (unsigned long int i = 1; i <= integers + 1; i++) {
unsigned long int r = rand() % ULONG_MAX;
bits.set(r - 1, true);
}

cout << getFirstMissingNumber(bits) << endl;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/interview_questions_4/missing-int.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
