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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:48:03+00:00 2026-05-18T21:48:03+00:00

Describe a data structure where: Any item is indexed by an integral value like

  • 0

Describe a data structure where:

  • Any item is indexed by an integral value like in an array
    • an integer can index a single value
    • integers used to index items are contiguous: they go from 1 to n without holes
  • Getting the item at position i (ie: the item associated to the integer i) should be as fast as possible
    • random access
  • Inserting a new item at position i should be as fast as possible
    • this will right-shift any item from i onwards
  • Removing an item at position i should also be as fast as possible
    • this will left-shift any item from i+1 onwards

EDIT: a little thing I forgot about: item indices can only be shifted when adding/removing one, they cannot be randomly swapped.

In this description n is the size of the structure (ie: how many items it contains), and i is a generic integer (1 <= i <= n), of course.


I heard this from a guy I met in my faculty. Don’t know if it’s an interview question, an exam question, just a riddle or what, but I guess it could be everything.

If I recall correctly (but hey, it was before December 24th) he said such a data structure could be implemented either with O(sqrt n) insertion/remotion and O(1) access time, or with O(log n) for any operation.


EDIT: Some right answers have been given. Read it if you don’t want to think any more about this problem.

  • 1 1 Answer
  • 1 View
  • 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-18T21:48:03+00:00Added an answer on May 18, 2026 at 9:48 pm

    Well, any kind of self-balancing binary tree (e.g., red-black) will provide all three operations for O(logn). C++ map RB mentioned is one example (if I didn’t forget C++ completely).

    As for the indexing (get operation), there’s a standard trick. We’ll store in each node how many nodes its left subtree has. Now we can locate element at position i in O(logn) time in a manner like this

    Data get(Node root, int i) {
        if (i <= root.leftCount) {
            return get(root.left, i);
        } else if (i == root.leftCount + 1) {
            return node;
        } else {
            return get(root.right, i - root.leftCount - 1);
        }
    }
    

    Obviously, each time element is added or removed, leftCount values will have to be recomputed, but that’ll need to be done only for O(logn) nodes. (think how many nodes include removed one in their left subtree – only the ones directly between it and root)

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

Sidebar

Related Questions

I encountered the following interview question on the Internet. Describe a data structure for
First, I'd like to describe my current situation. I have an RDLC whose data
I've seen the term intrusive used to describe data structures like lists and stacks,
Say that I have an integer-indexed array of length 400, and I want to
In phpMyAdmin under operations I can Copy database to: and select Structure and data
I have an XML file which describes the data-structure that I can exchange on
Has anyone ever heard of a general (textual) notation that can describe any IT
Easier to describe by showing a simplified view of the existing data structure and
As I understand it, Domain Models are classes that only describe the data (aggregate
I am using data-attributes in my portal page to describe favorite links. This is

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.