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 9200209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:42:51+00:00 2026-06-17T22:42:51+00:00

So I recently had this as an interview question and I was wondering what

  • 0

So I recently had this as an interview question and I was wondering what the optimal solution would be. Code is in Objective-c.

Say we have a very large data set, and we want to get a random sample
of items from it for testing a new tool. Rather than worry about the
specifics of accessing things, let’s assume the system provides these
things:

// Return a random number from the set 0, 1, 2, ..., n-2, n-1.
int Rand(int n);

// Interface to implementations other people write.
@interface Dataset : NSObject

// YES when there is no more data.
- (BOOL)endOfData;

// Get the next element and move forward.
- (NSString*)getNext;

@end


// This function reads elements from |input| until the end, and
// returns an array of |k| randomly-selected elements.
- (NSArray*)getSamples:(unsigned)k from:(Dataset*)input
{
  // Describe how this works.
}

Edit: So you are supposed to randomly select items from a given array. So if k = 5, then I would want to randomly select 5 elements from the dataset and return an array of those items. Each element in the dataset has to have an equal chance of getting selected.

  • 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-17T22:42:53+00:00Added an answer on June 17, 2026 at 10:42 pm

    This seems like a good time to use Reservoir Sampling. The following is an Objective-C adaptation for this use case:

    NSMutableArray* result = [[NSMutableArray alloc] initWithCapacity:k];
    
    int i,j;
    
    for (i = 0; i < k; i++) {
        [result setObject:[input getNext] atIndexedSubscript:i];
    }
    
    for (i = k; ![input endOfData]; i++) {
        j = Rand(i);
    
        NSString* next = [input getNext];
    
        if (j < k) {
            [result setObject:next atIndexedSubscript:j];
        }
    }
    
    return result;
    

    The code above is not the most efficient reservoir sampling algorithm because it generates a random number for every entry of the reservoir past the entry at index k. Slightly more complex algorithms exist under the general category “reservoir sampling”. This is an interesting read on an algorithm named “Algorithm Z”. I would be curious if people find newer literature on reservoir sampling, too, because this article was published in 1985.

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

Sidebar

Related Questions

This is one of an interview question which I had recently. I would like
I recently had this question in an interview and this is what I came
I recently had this question in an interview to write test cases to test
Recently i had an interview in which the question was asked as How would
I had heard this question from my friend who attended an interview recently: Given
I've had this behavior happen to me twice recently and I was wondering what
Recently, I had seen some D flip-flop RTL code in verilog like this: module
This task has already been asked/answered, but I recently had a job interview that
Recently I had this question , and everything worked properly until I sent it
I had recently asked a question regarding the following code: Sending Email in Android

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.