Data Structures and Algorithms in
Java (DSA)
Complete Study Guide and Reviewer
with a Multiple Choice Exam Questions with Verified Answers and
Rationale
Contents
• Part A: Study Guide (15 chapters, cheat sheets, review
checklist)
• Part B: Exam (150 multiple choice questions, each followed by
its answer and rationale)
•
Part A: Study Guide
Chapter 1: Java Fundamentals and OOP for DSA
• Primitives (int, long, double, boolean, char...) hold values;
references point to objects. Arrays and Strings are objects.
• Java is always pass-by-value. For objects, the reference is
copied, so methods can mutate an object but cannot rebind the
caller's variable.
• Use equals() for content comparison; == compares references
(and primitives). Override hashCode() whenever you override
equals().
• Generics accept only reference types: List<Integer>, not
List<int>. Autoboxing converts int to Integer.
DSA in Java Reviewer | Page 1
, • Integer cache: values -128 to 127 are cached, so Integer
a=1000, b=1000 gives a == b false.
• Comparable<T> gives natural ordering (compareTo).
Comparator<T> supplies external ordering (compare).
• final class = no subclass; final method = no override; final
variable = single assignment.
• Common exceptions: ArrayIndexOutOfBoundsException,
NullPointerException, ClassCastException, StackOverflowError.
class Node<T> {
T data;
Node<T> next;
Node(T d) { data = d; }
}
Chapter 2: Complexity Analysis
• Big-O = upper bound, Big-Omega = lower bound, Big-Theta =
tight bound.
• Drop constants and lower-order terms: 3n^2 + 5n + 7 is O(n^2).
• Growth order: 1 < log n < n < n log n < n^2 < n^3 < 2^n < n!.
• A loop whose variable doubles or halves runs O(log n) times.
Nested loops multiply.
• Amortized analysis averages cost over a sequence
(ArrayList.add is amortized O(1)).
• Space complexity includes the recursion call stack.
• Master theorem examples: T(n)=T(n/2)+O(1) gives O(log n);
T(n)=2T(n/2)+O(n) gives O(n log n); T(n)=2T(n/2)+O(1) gives
O(n).
DSA in Java Reviewer | Page 2
,for (int i = 1; i < n; i *= 2) { } // O(log n)
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++) { } // O(n^2)
Chapter 3: Arrays and ArrayList
• Arrays: fixed size, contiguous, O(1) index access, O(n)
insert/delete in the middle.
• ArrayList: dynamic array. add at end is amortized O(1);
add/remove in the middle is O(n); capacity grows about 1.5x.
• Arrays.sort(int[]) uses dual-pivot quicksort; Arrays.sort(Object[])
uses TimSort (stable).
• Useful utilities: Arrays.fill, Arrays.copyOf, Arrays.copyOfRange,
Arrays.toString, Arrays.deepToString, Arrays.equals.
• Jagged arrays: int[][] m = new int[3][]; each row is null until
allocated.
• Patterns: two pointers, sliding window, prefix sums, Kadane's
algorithm.
int[] a = {5, 2, 9};
Arrays.sort(a);
int[] b = Arrays.copyOf(a, 5); // pads 0
List<Integer> list = new ArrayList<>();
list.add(10);
list.remove(Integer.valueOf(10));
Tip: list.remove(int index) and list.remove(Object o) are different
overloads. Use Integer.valueOf(x) to remove by value.
Chapter 4: Linked Lists
• Singly: node has data and next. Doubly: data, prev and next.
Circular: last node points back to the first.
DSA in Java Reviewer | Page 3
, • Insert/delete at head is O(1); search is O(n); no random access.
• java.util.LinkedList is a doubly linked list that implements List
and Deque.
• Reverse a list with prev, curr, next pointers in O(n) time and
O(1) space.
• Floyd's cycle detection: slow moves 1, fast moves 2; they meet
if a cycle exists. Finding the middle uses the same pattern.
• Deleting a given non-tail node: copy next's data and unlink next.
Node reverse(Node head) {
Node prev = null, cur = head;
while (cur != null) {
Node nxt = cur.next;
cur.next = prev;
prev = cur;
cur = nxt;
}
return prev;
}
Chapter 5: Stacks
• LIFO. Operations push, pop, peek are O(1).
• Prefer ArrayDeque over legacy java.util.Stack (which extends
Vector).
• Applications: balanced parentheses, undo, expression
conversion and evaluation, DFS, monotonic stack (next greater
element).
• Infix to postfix: operands go straight out; operators wait on a
stack until a lower-precedence operator or a closing bracket
forces them out.
DSA in Java Reviewer | Page 4
Java (DSA)
Complete Study Guide and Reviewer
with a Multiple Choice Exam Questions with Verified Answers and
Rationale
Contents
• Part A: Study Guide (15 chapters, cheat sheets, review
checklist)
• Part B: Exam (150 multiple choice questions, each followed by
its answer and rationale)
•
Part A: Study Guide
Chapter 1: Java Fundamentals and OOP for DSA
• Primitives (int, long, double, boolean, char...) hold values;
references point to objects. Arrays and Strings are objects.
• Java is always pass-by-value. For objects, the reference is
copied, so methods can mutate an object but cannot rebind the
caller's variable.
• Use equals() for content comparison; == compares references
(and primitives). Override hashCode() whenever you override
equals().
• Generics accept only reference types: List<Integer>, not
List<int>. Autoboxing converts int to Integer.
DSA in Java Reviewer | Page 1
, • Integer cache: values -128 to 127 are cached, so Integer
a=1000, b=1000 gives a == b false.
• Comparable<T> gives natural ordering (compareTo).
Comparator<T> supplies external ordering (compare).
• final class = no subclass; final method = no override; final
variable = single assignment.
• Common exceptions: ArrayIndexOutOfBoundsException,
NullPointerException, ClassCastException, StackOverflowError.
class Node<T> {
T data;
Node<T> next;
Node(T d) { data = d; }
}
Chapter 2: Complexity Analysis
• Big-O = upper bound, Big-Omega = lower bound, Big-Theta =
tight bound.
• Drop constants and lower-order terms: 3n^2 + 5n + 7 is O(n^2).
• Growth order: 1 < log n < n < n log n < n^2 < n^3 < 2^n < n!.
• A loop whose variable doubles or halves runs O(log n) times.
Nested loops multiply.
• Amortized analysis averages cost over a sequence
(ArrayList.add is amortized O(1)).
• Space complexity includes the recursion call stack.
• Master theorem examples: T(n)=T(n/2)+O(1) gives O(log n);
T(n)=2T(n/2)+O(n) gives O(n log n); T(n)=2T(n/2)+O(1) gives
O(n).
DSA in Java Reviewer | Page 2
,for (int i = 1; i < n; i *= 2) { } // O(log n)
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++) { } // O(n^2)
Chapter 3: Arrays and ArrayList
• Arrays: fixed size, contiguous, O(1) index access, O(n)
insert/delete in the middle.
• ArrayList: dynamic array. add at end is amortized O(1);
add/remove in the middle is O(n); capacity grows about 1.5x.
• Arrays.sort(int[]) uses dual-pivot quicksort; Arrays.sort(Object[])
uses TimSort (stable).
• Useful utilities: Arrays.fill, Arrays.copyOf, Arrays.copyOfRange,
Arrays.toString, Arrays.deepToString, Arrays.equals.
• Jagged arrays: int[][] m = new int[3][]; each row is null until
allocated.
• Patterns: two pointers, sliding window, prefix sums, Kadane's
algorithm.
int[] a = {5, 2, 9};
Arrays.sort(a);
int[] b = Arrays.copyOf(a, 5); // pads 0
List<Integer> list = new ArrayList<>();
list.add(10);
list.remove(Integer.valueOf(10));
Tip: list.remove(int index) and list.remove(Object o) are different
overloads. Use Integer.valueOf(x) to remove by value.
Chapter 4: Linked Lists
• Singly: node has data and next. Doubly: data, prev and next.
Circular: last node points back to the first.
DSA in Java Reviewer | Page 3
, • Insert/delete at head is O(1); search is O(n); no random access.
• java.util.LinkedList is a doubly linked list that implements List
and Deque.
• Reverse a list with prev, curr, next pointers in O(n) time and
O(1) space.
• Floyd's cycle detection: slow moves 1, fast moves 2; they meet
if a cycle exists. Finding the middle uses the same pattern.
• Deleting a given non-tail node: copy next's data and unlink next.
Node reverse(Node head) {
Node prev = null, cur = head;
while (cur != null) {
Node nxt = cur.next;
cur.next = prev;
prev = cur;
cur = nxt;
}
return prev;
}
Chapter 5: Stacks
• LIFO. Operations push, pop, peek are O(1).
• Prefer ArrayDeque over legacy java.util.Stack (which extends
Vector).
• Applications: balanced parentheses, undo, expression
conversion and evaluation, DFS, monotonic stack (next greater
element).
• Infix to postfix: operands go straight out; operators wait on a
stack until a lower-precedence operator or a closing bracket
forces them out.
DSA in Java Reviewer | Page 4