Essential Java interview questions covering data structures and algorithms, focusing on logic, optimization, and computer science fundamentals.
Which approach can efficiently reverse a singly linked list in Java while minimizing space complexity?
Explanation: Iterative pointer manipulation reverses a singly linked list in-place with O(1) extra space. In-order tree traversal is unrelated to linked lists. HashMap look-up addresses key-value retrieval, not list reversal. Depth-first search is a tree/graph technique, not directly applicable for reversing linked lists.
What is the time complexity of accessing an element by index in a Java ArrayList?
Explanation: Accessing by index in an ArrayList is O(1) because it uses a backing array for direct lookup. O(n) would apply if scanning the list. O(log n) is typical for binary search. O(n^2) is not relevant for access operations in ArrayList.
Which scenario is best suited for using a Stack data structure in Java?
Explanation: Stacks operate in a last-in, first-out manner, ideal for undo features. Efficient random access is not possible as Stack does not support direct element access. Stable sorting and key-value retrieval are not core Stack use cases.
Which condition must be met for binary search to work correctly on a Java array?
Explanation: Binary search requires the array to be sorted to effectively split search ranges. Reversed order, unique elements, or power-of-two size are not necessary for binary search.
Which property distinguishes a Java HashSet from a List?
Explanation: HashSet stores unique items and does not allow duplicates. Unlike a List, it does not guarantee order or indexed access. It also does not store key-value pairs like a Map.