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
List
s 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.