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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:13:42+00:00 2026-05-21T09:13:42+00:00

I was given this problem in an interview. How would you have answered? Design

  • 0

I was given this problem in an interview. How would you have answered?

Design a data structure that offers the following operations in O(1) time:

  • insert
  • remove
  • contains
  • get random element
  • 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-05-21T09:13:43+00:00Added an answer on May 21, 2026 at 9:13 am

    Consider a data structure composed of a hashtable H and an array A. The hashtable keys are the elements in the data structure, and the values are their positions in the array.

    1. insert(value): append the value to array and let i be its index in A. Set H[value]=i.
    2. remove(value): We are going to replace the cell that contains value in A with the last element in A. let d be the last element in the array A at index m. let i be H[value], the index in the array of the value to be removed. Set A[i]=d, H[d]=i, decrease the size of the array by one, and remove value from H.
    3. contains(value): return H.contains(value)
    4. getRandomElement(): let r=random(current size of A). return A[r].

    since the array needs to auto-increase in size, it’s going to be amortize O(1) to add an element, but I guess that’s OK.

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

Sidebar

Related Questions

Recently in a job interview, I was given the following problem. Say I have
Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
This is a question that I was asked on a job interview some time
I got this problem from an interview with Microsoft. Given an array of random
Someone I know went to an interview and was given the following problem to
I was given the following problem in an interview: Given a staircase with N
I came across this problem during an interview forum., Given an int array which
Problem: Given this nested layout structure: ~/Views/Shared/_layoutBase.cshtml ~/Views/Shared/_layout.cshtml Where _layoutBase.cshtml is the layout for
In this earlier question the OP asked the following problem: Given a rectangular grid
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they

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.