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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:53:48+00:00 2026-05-25T22:53:48+00:00

What’s the best way to design such a container, which can support randomized value

  • 0

What’s the best way to design such a container, which can support randomized value access? but the container has to support other operations, such as insert key/value pairs and remove by key, with the best possible time performance.

One way to do this is to combine hash map with array, but if hash map is used, what’s the best way to do random access of hash map values, i.e., without generating a key.

  • 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-25T22:53:49+00:00Added an answer on May 25, 2026 at 10:53 pm

    If you’re talking about data structures, and not existing language support – then you just have to design a data structure to support that.

    You can do that, for example, by implementing a hash map which will hold, in addition, an array of pointers to its members. You can then translate random access operators to that array, and maintain it with every insertion or removal (that is of course a general idea, some implementation details omitted).

    Some languages support traversing the data structures through iterators. Although looping on an iterator for a random amount of times is not really random access (performance-wise), it will give you the same result in more time.

    I would say your question sounds like some algorithms’ coursework homework. Why would you want to do it in the real life? What is the problem you’re trying to solve?

    edit

    In the comments you phrased the problem as:

    what’s the best way to design such a container, which can support
    randomized value access? but the container has to support other
    operations, such as insert key/value pairs and remove by key, with the
    best possible performance.

    My suggestions above hold, but the question is what is the trade-off. If the “best performance” is time-wise, then my suggestion with the array gives you that. If the best performance is memory-wise then the iterating over the tree would give you that, that’s my other suggestion.

    In general, when you come to a need to design a new data structure, you need to answer the following questions:

    1. What are the operations required?
    2. What is the time complexity required, for each operation?
    3. What is the memory complexity required for the structure?
    4. Which is more important, memory or time?

    Sometimes you just can’t do in O(1) without additional memory. Sometimes you can do in O(1) with additional O(n) memory, but you can make it with O(lg n) memory if you compromise on O(lg n) time. There are trade offs that you have to make your decision about, I don’t know them.

    So my first suggestion (combining a BST or hash with an array of pointers to its nodes) does all the operations of the BST (map) or hash with the standard complexity of BST/hash operations, and all the read operations of array with the standard complexity (i.e.: random access in O(1) time). Write operations of the array will be with complexity of map/hash, and additional memory footprint is O(n).

    My second suggestion has no additional memory footprint, but the “random” access is pseudo random: you can just iterate to the point you want instead of directly accessing it. That would make your random access in O(n) while zero additional coding, or wasting memory.

    Name of the game? Trade-offs.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.