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

  • SEARCH
  • Home
  • 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 8316857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:27:08+00:00 2026-06-08T21:27:08+00:00

I am looking at the following interview question : Given 2d coordinates , find

  • 0

I am looking at the following interview question :

Given 2d coordinates , find the k points which are closest to the
origin. Propose a data structure for storing the points and the method to get the k points. Also point out the complexity of the code.

The solution that I have figured out is to save the 2d points in an array. For the first k points, find the distance of each point from the origin and build a max heap. For the remaining points , calculate distance from the origin , say dist. If dist is greater than the topmost element of the heap, then change topmost element of heap to dist and run the heapify() procedure.

This would take O(k) to build the heap and O((n-k)log k) for heapify() procedure , thus the total complexity = O(n log k).

Can anyone suggest a better data structure and/or method , with a possibly better efficiency too ?

EDIT

Would some other data structure be beneficial here ?

  • 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-08T21:27:10+00:00Added an answer on June 8, 2026 at 9:27 pm

    I would use order statistics for this one.
    Note that we use a modified SELECT that uses distance from the origin as the comparison function.

    • Store the elements in an array A, first element is A[1] and last is A[n].
    • Run SELECT(A,1,n,k) to find the kth closest element to the origin.
    • Return the elements A[1..k].

    One of the benefits of SELECTthat it partitions the input,
    so that the smallest k-1 elements are left to A[k].

    So storing the elements in an array is O(n).
    Running SELECT is O(n).
    Returning the requested elements is O(1).

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

Sidebar

Related Questions

I was asked the following Question: How would you store the data given below(which
I am looking at the following type of data structure: { author = Author
Currently, I am looking the following data structure. Fast to insert at tail. Fast
I was given the following question in an interview... Compute the following sum: 1/2
Looking in this StackOverflow question it uses the following to send emails: System.Net.Mail.SmtpClient Is
I'm looking for the following info: Full Phonegap reference (couldn't find it on their
I had an interview question asking this: text file has following lines> 1: A
Looking at the following code, does theme[sprite].img get nested inside result[definition].data (as theme[sprite].img is
Im looking at the following two ways of retrieving a value and storing it
In this question , I got the following answer: Looking at Google's maps script

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.