You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
List<Integer> arrayList = newArrayList<>();
List<Integer> linkedList = newLinkedList<>();
// Common operationslist.add(5); // Add elementlist.get(0); // Get by indexlist.remove(0); // Remove by indexlist.contains(5); // Check existencelist.size(); // Get size
Time Complexities
Operation
ArrayList
LinkedList
Access
O(1)
O(n)
Insert/Remove (end)
O(1)
O(1)
Insert/Remove (middle)
O(n)
O(n)
Search
O(n)
O(n)
Common Algorithms
Sliding window
Merge intervals
BFS queue (LinkedList)
Dynamic programming
🔸 HashMap
Characteristics
Key-value store with average O(1) access
No duplicate keys allowed
Use when quick lookup by key is required
Java Implementation
Map<String, Integer> map = newHashMap<>();
// Common operationsmap.put("apple", 3); // Insert/Updateintcount = map.get("apple"); // Get valuebooleanexists = map.containsKey("banana"); // Check keymap.remove("apple"); // Removeintsize = map.size(); // Get size
Common Use Cases
Counting frequencies
Caching/memoization
Finding complements (two-sum problem)
Grouping elements
Time Complexities
Operation
Complexity
Description
Put
O(1) average
Insert/update key-value pair
Get
O(1) average
Retrieve value by key
Remove
O(1) average
Remove key-value pair
Contains Key
O(1) average
Check if key exists
🔸 HashSet
Characteristics
No duplicates, no order
Internally backed by a HashMap
Use when you need uniqueness
Java Implementation
Set<Integer> set = newHashSet<>();
// Common operationsset.add(10); // Add elementbooleanexists = set.contains(10); // Check existenceset.remove(10); // Remove elementintsize = set.size(); // Get size
Common Use Cases
Checking for duplicates
Set intersections
Tracking seen elements in sliding window problems
Finding unique elements
Time Complexities
Operation
Complexity
Description
Add
O(1) average
Add element
Contains
O(1) average
Check if element exists
Remove
O(1) average
Remove element
🔸 Stack
Characteristics
LIFO (Last In, First Out) data structure
Use for problems involving nested structures or backtracking
Java Implementation
Stack<Integer> stack = newStack<>();
// Common operationsstack.push(5); // Add to topinttop = stack.pop(); // Remove from topintpeek = stack.peek(); // View top without removingbooleanempty = stack.isEmpty(); // Check if empty
Common Use Cases
Expression evaluation
Undo operations
Function call management
Balanced parentheses problems
🔸 Queue
Characteristics
FIFO (First In, First Out) data structure
Use for BFS and level-order traversals
Java Implementation
Queue<Integer> queue = newLinkedList<>();
// Common operationsqueue.offer(5); // Add to endintfront = queue.poll(); // Remove from frontintpeek = queue.peek(); // View front without removingbooleanempty = queue.isEmpty(); // Check if empty
Common Use Cases
BFS (Breadth-First Search)
Level-order tree traversal
Task scheduling
Buffer management
✅ Quick Reference - When to Use
Data Structure
Use When...
Time Complexity
Array
You know the exact number of items
Access: O(1), Search: O(n)
ArrayList
Need dynamic array, fast access by index
Access: O(1), Insert: O(n)
LinkedList
Need frequent insertions/removals at ends
Insert/Remove: O(1), Access: O(n)
HashMap
Fast lookups by key
All operations: O(1) average
HashSet
Need uniqueness, check if element seen
All operations: O(1) average
StringBuilder
Repeated string modifications
Append: O(1), toString: O(n)
Stack
Nested structures, backtracking
Push/Pop: O(1)
Queue
BFS, level-order processing
Offer/Poll: O(1)
🎯 Algorithm Problem Patterns
Two Pointers
Use: Arrays, Strings
Pattern: Start with pointers at both ends or both at start
Examples: Two Sum, Valid Palindrome, Container With Most Water
Sliding Window
Use: Arrays, Strings
Pattern: Maintain a window of elements
Examples: Longest Substring Without Repeating Characters, Maximum Sum Subarray
Hash Map/Dictionary
Use: Frequency counting, complement finding
Pattern: Store processed elements for quick lookup
Examples: Two Sum, Group Anagrams, Longest Consecutive Sequence
Stack
Use: Nested structures, matching problems
Pattern: Process elements in LIFO order
Examples: Valid Parentheses, Daily Temperatures, Largest Rectangle in Histogram