I came across this puzzle [ related to data structure ] question in a coding contest.
There is a planet of trees (real trees, not a tree structure!). It has billions or even trillions of trees. The king orders to find the median epoch (in years and whole) of all trees, using, for example, carbon dating. ( Method does not matter. ) Note. Median is the "average" in a sorted list of numbers.
Limitations:
1. The oldest tree, as you know, 2000 years old.
2. They have one machine that can store integers ranging from -infinity to + infinity.
3. But the number of such integers that can be stored in memory at the same time is 1 million.
therefore, as soon as you store 1 million integers to store the next, you must delete the already saved file.
So for some reason they have to keep track of the medians as they continue to count age-old trees.
How can they do this?
My approach
Use the external sort option to sort the ages in pieces and write them to a file.
Apply k-way merging [for pieces].
The problem with the above approach is that this requires two file scans.
I can think of another approach that uses the information. The oldest tree is known to be 2000 years old.
Can't we take a count array [ as range of ages of tree is fixed ]?
I want to know if there is a better approach?
Is there any method in which we do not need to store data in a file? [ where only main memory is sufficient? ]
Green goblin
source share