0

According to me:

Indexed Based: Which can be accessed by passing index. Now internally it is doing random or sequential iteration doesnt matter.

Random Access: You can access a position in on go randomly.

Sequential Access: The desired position is to be accessed sequentially one by one starting from some other position. But when in interviews I say LinkedList is indexed based in java because it provides all the methods add(int index, obj), get(int index), remove(int index). People dont accept. Then I say indexed based and random access are two different concepts. Am I right?
Ankur
  • 59
  • 2
  • 8
  • Just because you can look up an element by position doesn't make a data structure index-based. A linked list doesn't do anything that resembles "indexing" by any definition I've run across. – azurefrog Mar 04 '17 at 17:23
  • @azurefrog, you mean other than what "[T]he List interface provides[,] four [sic] methods for positional (indexed) access to list elements", namely `add(int index, E element)`, `addAll(int index, Collection extends E> c)`, `get(int index)`, `indexOf(Object o)`, `contains`, `remove`, `set`, and `subList`, of course. – Lew Bloch Mar 04 '17 at 17:51
  • Before you guys start arguing bear in mind that "implementation" (azurefrog) and "interface" (Lew) are two different things. You might want to make sure you're both on the same page first. – Jason C Mar 04 '17 at 17:54
  • @azurefrog: Ok so according to you what is indexing? – Ankur Mar 05 '17 at 13:45
  • The creation or use of an "index" as a proxy of actual data to speed up data lookup. An example would be creating an index column in a database. Another would be creating a lookup table against an in-memory data structure. Another would be storing keywords associated with web pages to enable search engine indexing. The common theme always being similar to that of a book's index: to facilitate faster lookup of data. – azurefrog Mar 06 '17 at 04:10
  • What linkedlist using to get(int index)is:public E get(int index) {checkElementIndex(index); return node(index).item; } Node node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; i guess its sequential.....}http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/java/util/LinkedList.java – Ankur Mar 06 '17 at 17:59
  • @azurefrog: what do you say on above comment? – Ankur Mar 07 '17 at 15:28
  • 1
    I would say that you asked for my definition and I gave it to you. Feel free to disagree, but keep in mind that (as you point out in your original question) your definition is not commonly accepted. See @JasonC's answer for a good way to handle that in interviews. – azurefrog Mar 07 '17 at 18:24

3 Answers3

11

So, this question is slightly trickier than it seems because the appropriateness of these terms does depend a bit on context, and as JB Nizet highlights in a comment below, this really boils down to a terminology question moreso than a question of implementations or actual concepts, and so this answer is mostly pedantry about phrasing. First of all, your definitions are:

  • Indexed Based: Which can be accessed by passing index. Now internally it is doing random or sequential iteration doesnt matter.
  • Random Access: You can access a position in on go randomly.
  • Sequential Access: The desired position is to be accessed sequentially one by one starting from some other position.

Taking care of this part first: "Indexed-based" doesn't really belong there. The term "index-based" doesn't really identify an access type or anything, it just means... "based on indices". It's just an arbitrary adjective that can describe something. When talking about general access types, we only really talk about random vs. sequential. Typically "index-based access" implies / is a synonym for "random access". If a "random access" style interface is present, maybe it uses indices, maybe it uses something else, who knows, it doesn't really matter. So let's cross "index-based" off your list of terms there.

If that doesn't quite make sense, think of it this way: Your list "index based", "random access", "sequential access", is sort of analogous to "magnetic", "metal chair", "wooden chair", respectively. The first is just an adjective, not a type of chair, and if you're talking about chairs then it would imply "metal chair" and wouldn't make sense with "wooden chair".

Now, as for LinkedList, it's easier just to express a few points as a list, in no particular order:

  • LinkedList implements the List interface, and thus supports random access (via get(int) and friends).
  • LinkedList also supports sequential access (via iterator()) from the Collection interface (which List extends).
  • It follows that all Lists in Java (be it LinkedList, ArrayList, etc.) support both random and sequential access.

So it is correct to say that LinkedList supports (or "allows for" or whatever words you like) random access, and that it supports sequential access.

On the other hand, you could talk about the complexity/implementation details:

  • LinkedList is ideal for sequential access, because of the nature of a linked list.
  • LinkedList random access is O(n) worst case, as it must be implemented via sequential iteration. You cannot jump to a specific index, rather, you must start at the beginning and iterate through.

So:

But when in interviews I say LinkedList is indexed based in java because it provides all the methods add(int index, obj), get(int index), remove(int index). People dont accept. Then I say indexed based and random access are two different concepts. Am I right?

Not exactly. As mentioned above "indexed-based access" is often a synonym for "random access" or at least implies it, but "indexed-based" isn't a thing on its own. There is only "random" and "sequential" here.

What you can say about a LinkedList is:

  • It supports index-based random access.
  • It does not have good random access performance compared to, say, an ArrayList.
  • It is ideal for sequential access.

What you can't say is that a LinkedList "is index-based". While List's random access interface is index-based, it doesn't make much sense to say that "the implementation of a linked list is index-based", or that "a linked list is index-based", because linked lists aren't based on indices, and this is independent of the fact that Java's implementation happens to provide both random and sequential access interfaces.

Also, you don't really say "a linked list is random access" or "a linked list is sequential access". Semantically, these phrases don't make a lot of sense. A linked list is a linked list, it has poor random access performance, and Java provides both a random and sequential access interface to it, but the list itself isn't usually said "to be" one of those things.

So your interview answer could have been "Well, a LinkedList supports index-based random access through the List interface, but its performance isn't as good as an ArrayList and sequential access is more ideal."

Hope that helps.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    An interesting point to mention is that Java has an interface named RandomAccess, which is actually used to mark List implementations having *fast* random access (typically O(1), as explained in the documentation). To the OP, I'd say that it's mainly a terminology question. Terminology is important, but less, IMHO, than understanding the time complexity of the collections you're using. If you're able to exlain what *you* mean with these terms (and it seems you are), and the interviewer rejects you over terminology subtleties, maybe it wasn't a good place to work. – JB Nizet Mar 04 '17 at 17:44
  • @JBNizet *"If you're able to exlain what you mean with these terms (and it seems you are), and the interviewer rejects you over terminology subtleties, maybe it wasn't a good place to work."* -- Cheers to that. – Jason C Mar 04 '17 at 17:45
  • @JBNizet and Jason C ..Yeah that should be the spirit. – Ankur Mar 05 '17 at 13:57
  • 1
    'LinkedList random access is O(n) worst case', I believe it's actually O(n/2) as it starts from whichever end of the LinkedList is closer to the index passed, being a Doubly-Linked LIst :-) – Zippy May 11 '18 at 00:07
1

You're partly correct. "Random" access is misnamed; it's really arbitrary access, in other words, the same as indexed access. Indexed and sequential access are different, but not mutually exclusive. We say they are orthogonal; you can have neither, either, or both.

Indexed ("random") access means you can reach any position at will, such as (pseudocode) knickknacks[17] or knickknacks["paddywhack"].

Sequential access means a traversal across the collection one by one from some start location to some end.

for (Paddywhack aBone : knickknacks) {
  give(theDog, aBone);
}

List instances support both modes. Set instances support indexed access for contains and remove; sequential access only for retrieval via iterator and for-each loops.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
  • 1
    The phrase "indexed sequential access" would not make sense to any programmer, though. – Jason C Mar 04 '17 at 17:41
  • Because access is one or the other at a time, but support for both can exist in the same structure. – Lew Bloch Mar 04 '17 at 18:36
  • @JasonC: Linked List does not implement RandomAccess, but supports Index access. However ArrayList implements RandomAccess and support index access too. Linked list: http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.htm – Ankur Mar 05 '17 at 15:01
  • 2
    @Ankur Just to be clear, the `RandomAccess` interface is only an indicator that a container provides *efficient* random access (note that that interface adds no new methods, it is only a marker). It is rather confusingly named for that reason. The `List` interface's `get(int)` etc. always provides random access, unrelated to whether or not `RandomAccess` is implemented. That `LinkedList` does not implement `RandomAccess` doesn't mean it doesn't *support* random access, it just means the provided random access is not particularly efficient. – Jason C Mar 05 '17 at 16:27
  • 1
    @Ankur there's a difference between supporting random access and implementing `java.util.RandomAccess`. Also, "random access" is not random, as pointed out; it means indexed access. Also, when you cite Javadocs you might wish to cite the current version, not one whose End of Life was two and a half years ago. – Lew Bloch Mar 05 '17 at 18:22
  • What linkedlist using to get(int index)is:public E get(int index) {checkElementIndex(index); return node(index).item; } Node node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; i guess its sequential.....}http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/java/util/LinkedList.java – Ankur Mar 06 '17 at 18:02
  • The functionality of `get(int index)` is indexed. The underlying implementation for some types is sequential. The Law of Demeter restricts knowledge of internals, so the client experiences what the contract calls for. Programming is a lot about distinguishing the semantics of different levels of analysis. One such distinction is that between external interface and internal implementation. – Lew Bloch Mar 06 '17 at 18:53
  • @JasonC So you agree that in LinkedList index based searches are being done sequentially and its possible. So indexed based searching can be acheived by both randomly and sequentially. My saying is just indexing is just giving numeric identification to things...and it can either be accessed randomly or sequentially. But people had a predisposition that indexed means random. and java implementation of linkedlist denies it. – Ankur Mar 07 '17 at 15:26
  • 1
    @Ankur Well, I think you're blending together some concepts a bit. Random *access* and sequential *access* are just ways to *access* data. This is *completely independent* of the data structure you are using underneath. Imagine just operating on the `List` interface for example, this illustrates the independence: You can access the data both randomly and sequentially and it doesn't matter if it's a `LinkedList` or an `ArrayList` or something else. – Jason C Mar 07 '17 at 16:01
  • 1
    @Ankur It's sort of like the comments under your original question: If it's not clear whether you're talking about implementation (/ performance) vs. interface, there *will* be miscommunication. Note also that in any case regarding containers, if you have both sequential access and the ability to reset back to the first element, you can kludge random access on top of it. Likewise if you have random access, you can implement sequential access with an iterator that tracks the "current" index. These are just *ways to get at the data in the container* and nothing more. – Jason C Mar 07 '17 at 16:03
  • @Ankur (Last comment, heh): As for the *implementation*, this is just a detail behind the interface. E.g., to implement random access to a linked list, sure, you have to start at the head node and sequentially move through it. And to implement sequential access to an array, sure, you have to make the iterator behave sequentially by maintaining the current index and incrementing it. But the *interface* is the same. The *implementation* depends on the nature of the data structure, but *you can still support both types of access*. Trying to talk about both things at once will lead to confusion. – Jason C Mar 07 '17 at 16:10
  • 1
    The confusion vanishes if one is clear about whether the topic is the external interface (in general terms, often embodied in a Java `interface`), or the internal implementation. Most of the time it's the external interaction that signifies when discussing this kind of thing. – Lew Bloch Mar 07 '17 at 17:01
0

Here you can point out the logic and also refer Image as well....

ArrayList-Random(Indexed) access, Pass random index and get element.

LinkedList-Sequential access, Get first/last element which will have reference to next/previous element.

enter image description here

Siddappa Walake
  • 303
  • 5
  • 14