我的面试问题是这样的:

给定一个包含40亿个整数的输入文件,提供一种算法来生成一个文件中不包含的整数。假设您有1gb内存。如果你只有10mb的内存,你会怎么做。

我的分析:

文件大小为4×109×4 bytes = 16gb。

我们可以进行外部排序,从而知道整数的范围。

我的问题是,在已排序的大整数集中检测缺失整数的最佳方法是什么?

我的理解(看完所有答案后):

假设我们讨论的是32位整数,有232 = 4*109个不同的整数。

情况1:我们有1gb = 1 * 109 * 8位= 80亿位内存。

解决方案:

如果我们用一位表示一个不同的整数,这就足够了。我们不需要排序。

实现:

int radix = 8;
byte[] bitfield = new byte[0xffffffff/radix];
void F() throws FileNotFoundException{
    Scanner in = new Scanner(new FileReader("a.txt"));
    while(in.hasNextInt()){
        int n = in.nextInt();
        bitfield[n/radix] |= (1 << (n%radix));
    }

    for(int i = 0; i< bitfield.lenght; i++){
        for(int j =0; j<radix; j++){
            if( (bitfield[i] & (1<<j)) == 0) System.out.print(i*radix+j);
        }
    }
}

情形二:10mb内存= 10 * 106 * 8bits = 8000万bits

Solution: For all possible 16-bit prefixes, there are 216 number of integers = 65536, we need 216 * 4 * 8 = 2 million bits. We need build 65536 buckets. For each bucket, we need 4 bytes holding all possibilities because the worst case is all the 4 billion integers belong to the same bucket. Build the counter of each bucket through the first pass through the file. Scan the buckets, find the first one who has less than 65536 hit. Build new buckets whose high 16-bit prefixes are we found in step2 through second pass of the file Scan the buckets built in step3, find the first bucket which doesnt have a hit. The code is very similar to above one.

结论: 我们通过增加文件传递来减少内存。


为那些迟到的人澄清一下:这个问题,正如所问的,并不是说有一个整数没有包含在文件中——至少大多数人不是这么理解的。不过,评论线程中的许多评论都是关于任务的变化。不幸的是,把它引入评论线程的评论后来被它的作者删除了,所以现在看起来它的孤儿回复只是误解了一切。这很让人困惑,抱歉。


当前回答

如果我们假设数字的范围总是2^n(2的偶次幂),那么排异或将成立(如另一张海报所示)。至于为什么,让我们证明一下:

这个理论

给定任何以0为基础的整数范围,其中有2^n个元素,其中缺少一个元素,您可以通过简单地将已知值乘在一起来找到缺少的元素。

证明

我们看一下n = 2。对于n=2,我们可以表示4个唯一的整数:0、1、2、3。它们有一个模式:

0-00 1-01 2-10 3-11

现在,如果我们仔细观察,每个位都被精确地设置了两次。因此,由于它被设置为偶数次,而这些数字的异或将产生0。如果缺少一个数字,排他性或将产生一个数字,当与缺少的数字排他性时,结果将为0。因此,丢失的号码和得到的排他号码完全相同。如果去掉2,得到的xor将是10(或2)。

现在来看n+1。让我们称每个比特在n中被设置的次数为x,称每个比特在n+1中被设置的次数为y。y的值将等于y = x * 2,因为有x个元素的n+1位设置为0,x个元素的n+1位设置为1。因为2x总是偶数,所以n+1总是将每一位设置为偶数次。

因此,既然n=2可行,n+1也可行,那么xor方法将适用于所有n>=2的值。

0为基础范围的算法

这很简单。它使用2*n位内存,因此对于任何<= 32的范围,2个32位整数都可以工作(忽略文件描述符消耗的任何内存)。它只对文件进行一次传递。

long supplied = 0;
long result = 0;
while (supplied = read_int_from_file()) {
    result = result ^ supplied;
}
return result;

任意基范围的算法

该算法适用于任何起始数到任何结束数的范围,只要总范围等于2^n…这基本上是重新定义了范围,使最小值为0。但它确实需要遍历文件2次(第一次获取最小值,第二次计算缺少的int)。

long supplied = 0;
long result = 0;
long offset = INT_MAX;
while (supplied = read_int_from_file()) {
    if (supplied < offset) {
        offset = supplied;
    }
}
reset_file_pointer();
while (supplied = read_int_from_file()) {
    result = result ^ (supplied - offset);
}
return result + offset;

任意范围

我们可以将这种修改后的方法应用于任意范围的集合,因为所有范围都会至少穿过2^n的幂次。这只适用于有一个单一的缺失位。它需要对一个未排序的文件进行2次传递,但它每次都会找到一个丢失的数字:

long supplied = 0;
long result = 0;
long offset = INT_MAX;
long n = 0;
double temp;
while (supplied = read_int_from_file()) {
    if (supplied < offset) {
        offset = supplied;
    }
}
reset_file_pointer();
while (supplied = read_int_from_file()) {
    n++;
    result = result ^ (supplied - offset);
}
// We need to increment n one value so that we take care of the missing 
// int value
n++
while (n == 1 || 0 != (n & (n - 1))) {
    result = result ^ (n++);
}
return result + offset;

基本上,在0附近重新设置范围。然后,它在计算异或时计算要追加的未排序值的数量。然后,它将未排序值的计数加1,以处理缺少的值(计算缺少的值)。然后,继续求n的值,每次加1,直到n是2的幂。然后将结果重新基于原始基数。完成了。

下面是我在PHP中测试的算法(使用数组而不是文件,但概念相同):

function find($array) {
    $offset = min($array);
    $n = 0;
    $result = 0;
    foreach ($array as $value) {
        $result = $result ^ ($value - $offset);
        $n++;
    }
    $n++; // This takes care of the missing value
    while ($n == 1 || 0 != ($n & ($n - 1))) {
        $result = $result ^ ($n++);
    }
    return $result + $offset;
}

在一个具有任何范围的值的数组中(我测试了包括负号),其中一个在该范围内是缺失的,它每次都找到正确的值。

另一种方法

既然我们可以使用外部排序,为什么不只是检查间隙呢?如果我们假设文件在运行这个算法之前已经排序:

long supplied = 0;
long last = read_int_from_file();
while (supplied = read_int_from_file()) {
    if (supplied != last + 1) {
        return last + 1;
    }
    last = supplied;
}
// The range is contiguous, so what do we do here?  Let's return last + 1:
return last + 1;

其他回答

检查输入文件的大小,然后输出任何过大而无法用该大小的文件表示的数字。这似乎是一个廉价的技巧,但它是一个创造性的解决面试问题的方法,它巧妙地避开了记忆问题,从技术上讲,它是O(n)。

void maxNum(ulong filesize)
{
    ulong bitcount = filesize * 8; //number of bits in file

    for (ulong i = 0; i < bitcount; i++)
    {
        Console.Write(9);
    }
}

应该打印10位计数- 1,这将永远大于2位计数。从技术上讲,你必须打败的数字是2 bitcount -(4 * 109 - 1),因为你知道文件中还有(40亿- 1)个其他整数,即使使用完美的压缩,它们也会占用至少1位。

出于某种原因,当我读到这个问题时,我想到了对角化。假设是任意大的整数。

Read the first number. Left-pad it with zero bits until you have 4 billion bits. If the first (high-order) bit is 0, output 1; else output 0. (You don't really have to left-pad: you just output a 1 if there are not enough bits in the number.) Do the same with the second number, except use its second bit. Continue through the file in this way. You will output a 4-billion bit number one bit at a time, and that number will not be the same as any in the file. Proof: it were the same as the nth number, then they would agree on the nth bit, but they don't by construction.

根据原题中目前的措辞,最简单的解决方法是:

找到文件中的最大值,然后加上1。

The simplest approach is to find the minimum number in the file, and return 1 less than that. This uses O(1) storage, and O(n) time for a file of n numbers. However, it will fail if number range is limited, which could make min-1 not-a-number. The simple and straightforward method of using a bitmap has already been mentioned. That method uses O(n) time and storage. A 2-pass method with 2^16 counting-buckets has also been mentioned. It reads 2*n integers, so uses O(n) time and O(1) storage, but it cannot handle datasets with more than 2^16 numbers. However, it's easily extended to (eg) 2^60 64-bit integers by running 4 passes instead of 2, and easily adapted to using tiny memory by using only as many bins as fit in memory and increasing the number of passes correspondingly, in which case run time is no longer O(n) but instead is O(n*log n). The method of XOR'ing all the numbers together, mentioned so far by rfrankel and at length by ircmaxell answers the question asked in stackoverflow#35185, as ltn100 pointed out. It uses O(1) storage and O(n) run time. If for the moment we assume 32-bit integers, XOR has a 7% probability of producing a distinct number. Rationale: given ~ 4G distinct numbers XOR'd together, and ca. 300M not in file, the number of set bits in each bit position has equal chance of being odd or even. Thus, 2^32 numbers have equal likelihood of arising as the XOR result, of which 93% are already in file. Note that if the numbers in file aren't all distinct, the XOR method's probability of success rises.

对于1gb RAM的变体,您可以使用位向量。你需要分配40亿比特== 500 MB字节数组。对于从输入中读取的每个数字,将相应的位设置为“1”。一旦你完成了,遍历比特,找到第一个仍然是“0”的比特。它的索引就是答案。