Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As other people have noted, you can use an O(n) <a href="http://en.wikipedia.org/wiki/Counting_sort" rel="noreferrer">counting sort</a>. However there are some additional problems you need to consider. We'll assume you're storing 32-bit integers, so 100GB ~ 27e9 ints.</p> <p>If all the integers are the same, then it will occur ~27e9 times, which is larger than a 32 bit int. Therefore your counters will have to be 64-bit integers.</p> <p>With 2GB of RAM, you can only store ~125e6 counters in RAM at any one time. If we can't make any assumptions about the distribution of integers, we would either have to:</p> <ul> <li>individually increment the counters on the HD, or </li> <li>ignore all integers we encounter that are not in the counter array we currently have stored in RAM.</li> </ul> <p>I think the latter option is better. Since we need ~4e9 64-bit counters and can only store 2GB, we would need to run through the entire array ~16 times. The first option is clearly no good if we consider encountering a sequence of integers such as 0,1&lt;&lt;31,0. These counters would not be stored in RAM at the same time, and thus at least 2 HD writes are required.</p> <p>Because of this, I think for the specific size of your problem (100GB), an <a href="http://en.wikipedia.org/wiki/External_sorting" rel="noreferrer">N-way merge</a> sort would be better, as this would only require reading the entire array log_2(100) ~ 8 times.</p> <p>However, if the interviewer immediately changed the question to "10TB array, still 2GB of RAM", then the counting sort would easily win.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload