4

I was asked this question in one of my interviews.

Is there a data structure which has the following 2 capabilities:

1. Constant time access (Random Access), like ArrayList

2. Variable size, like LinkedList

If there is no such data structure, create one on your own.

1
  • 5
    Er, ArrayList is variable size (?) Commented Mar 19, 2011 at 21:31

4 Answers 4

6

I believe the answer you're looking for is "Hash table" (See: "Hash Table" in Wikipedia) as you've commented that they were looking for another one beyond ArrayList (For Java, see: Hashtable)

Though be aware that it can be near constant time depending on the hashing algorithm and data set as collisions may occur resulting in a (short) secondary linear search. The Javadoc gives a really good explanation of how this is handled in the Java implementation.

Sign up to request clarification or add additional context in comments.

3 Comments

Or alternatively HashMap
@Zack - absolutely. I've edited to make more clear I was trying to describe the generic data structure, then give a java implementation example.
I understand :-). Just instinct when I see "Hashtable" to mention the HashMap.
3

In addition to the HashTable as Brian Roach said, the interviewer may have been alluding to a LinkedHashMap or LinkedHashSet, which provides about constant time performance for some basic operations, while also maintaining order of insertion, as it combines a HashTable with a doubly-linked list.

In other words, the order you put elements into the LinkedHashMap will be the same order they are retrieved if you loop over the keys.

One downside with using Sets though is you can't have duplicates, and Maps likewise can't have duplicate keys. The workaround would be having a Set of Lists, or using something like Google's Multimap.

But as everyone else said, an ArrayList already meets both requirements. It's an array that doesn't have variable size.

Plus, the major advantage of a LinkedList over an ArrayList is constant time insertion/deletion at both the end and beginning of the list, compared to ArrayList's O(n) performance. Both can provide variable size.

Comments

1

How about a deque, or a "double-ended queue"? In many implementations, it doesn't change size efficiently for insertions and removals at any location but its head or tail, but it otherwise provides the two properties you seek:

  • Growth with only occasional and minimal reallocation as elements are added
  • Constant-time access, as the deque is usually stored as an array of arrays

You mention Java in your question—by way of a tag—and I must note that the Deque interface in Java does not offer random access. Even the ArrayDeque concrete class doesn't offer it. That's an unfortunate choice, made in the interest of being more abstract and imposing as few requirements on the interface as possible.

Regardless, beyond Java, you will find deque implementations that do satisfy the properties you requested.

1 Comment

Any implementation using a doubly-linked list is going to have random access of O(n) except to the head or tail, which is indeed what you get with the Java implementation.
0

Why not have one hashtable and insert all elements in it while inserting element in LinkedList and read from hashtable in constant time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.