Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:49:53+00:00 2026-06-06T08:49:53+00:00

I faced this puzzle question[ related to data structure ] in a coding competition.

  • 0

I faced this puzzle question[related to data structure] in a coding competition.

There is a planet of trees (real trees not tree data structure!!). It has billions or even trillions of trees. The king orders to find the median of ages (in years and integers) of all the trees using say carbon dating. (Method does not matter.)
Note: The Median is the “middle number” in a sorted list of numbers.

Constraints:
1. The oldest tree is known to be 2000 years old.
2. They have single machine which can store integers in range from -infinity to +infinity.
3. But the number of such integers that can be stored in memory at a time is 1 million.

so, once you store 1 million integers to store next one you must delete already stored one.
So somehow they have to keep track of median as they go on counting the ages of trees.
How can they do this?

My approach
Use a variant of external sort to sort the ages in chunks & write them in file.
Apply k-way merging[for the chunks].
The problem with above approach is that it needs two scan of the file.

I can think of another approach which uses the information The oldest tree is known to be 2000 years old.
Cannot we take a count array [as range of ages of tree is fixed]?

I want to know is there any better approach?
Does there exist any method where we do not need to store the data in the file?[where only main memory is sufficient?]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:49:54+00:00Added an answer on June 6, 2026 at 8:49 am

    You can do this by storing just 2001 integers. Create an array of different possible ages

    ages[2001] // [0..2000]
    

    when you count a tree

    ages[thisAge]++
    

    Then computing the median is trivial.
    You seem to have hit on this solution in the second approach you mention, but then you say
    I want to know is there any better approach?

    Does there exist any method where we do not need to store the data in
    the file?[where only main memory is sufficient?]

    I don’t undertstand why you ask if there exists any method where main memory is sufficient. Doesn’t an array of 2001 integer fit in main memory?

    Using the approach above, you can fill your array of counts, and then calculate the median by iterating through the counts, keeping a sum total as you go. When your sum reaches half the total number of trees, you have the median. This requires one pass through all the trees to count, plus a pass through part of the count array of some number <=2001. So this is O(n). You could, instead, keep track of the median with this array as you go, but it would not really improve on the solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an interview question I faced recently. Given an array of 1 and
I have faced this question in my Interview as well. I do have many
Please, help! Does anybody faced this problem? -bash: make: command not found
I'm working on a project for school, and faced this elementary problem (it's not
I faced this strange situation where foreach like construct of javascript does not work
I faced this question in interview. When I searched on internet I found different
Forgive me if this question is very vague Some time back i faced this
I faced this problem how can I retrieve string with another language (not English)
This is not a unique problem that has not been there in the past,
I was faced this question while one of recent interview : class1 { virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.