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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:48:17+00:00 2026-06-01T09:48:17+00:00

I encountered the following interview question on the Internet. Describe a data structure for

  • 0

I encountered the following interview question on the Internet.

Describe a data structure for which getValue(int index), setValue(int index, int value), and setAllValues(int value) are all O(1).

Though array is good enough for the first and second operations to be performed in O(1), what can be proposed for the third (setAllValues)?

  • 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-01T09:48:18+00:00Added an answer on June 1, 2026 at 9:48 am

    How about an array of tuples {timestamp, value}, with an additional {timestamp, value} called all. Since you only care about the relative time of insertions, you can use a monotonically increasing id for the values of timestamp:

    type Entry {
      int timestamp, 
      int value
    }
    
    type structure {
        int     id
        Entry   all
        Entry[] array
    }
    

    Initialise all fields to 0. Then the following should work for you:

    • setValue(index i, value v):

      array[i] = {id++, value}
      
    • value getValue(index i)

      if(all.timestamp > array[i].timestamp) return all.value
      else return array[i].value
      
    • setAll(value v)

      all = {id++, value}
      

    A problem with this approach is that eventually you’ll run out of ids for timestamp, and might wrap around. If you chose a 64 bit value to store timestamps, then this gives you 18,446,744,073,709,551,616 insertions or setAlls before this happens. Depending on the expected use of the datastructure, an O(n) cleanup phase might be appropriate, or you could just throw an exception.

    Another issue that might need to be considered is multi-threading. Three obvious problems:

    • if id++ isn’t atomic and two threads obtained a new id at the same time then you could get two entries with the same id.
    • if the incrementation of id and the assignment of the created tuple aren’t atomic together (they’re probably not) and there were two simultaneous inserts, then you could get a race condition where the older id wins.
    • if the assignment of the tuple isn’t atomic, and there’s an insert() at the same time as a get() then you might end up in a position where you’ve got say {new_id, old_value} in the array, causing the wrong value to be returned.

    If any of these are problems, the absolute easiest solution to this is to put “not thread safe” in the documentation (cheating). Alternatively, if you can’t implement the methods atomically in your language of choice, you’d need to put some sort of synchronisation locks around them.

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

Sidebar

Related Questions

I recently encountered the following interview question: How can you multiply a number by
I encountered the following ddl in a pl/sql script this morning: create index genuser.idx$$_0bdd0011
I encountered the following problem with MongoID. I've created two models which related by
I encountered the following error when I was executing my application: java.sql.SQLException: No value
I have encountered the following problem which proved to me that I know far
I am in the process of changing my git-merge-workflow and encountered following problem: Up
I encountered the following error whilst running bundle install with ruby-debug19 in my Gemfile:
I've encountered the following paragraph: Debug vs. Release setting in the IDE when you
We've encountered the following situation in our database. We have table 'A' and table
I'm learning C# and I encountered the following problem. I have two classes: base

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.