JEE Pattern: 1-2 questions on arrays, linked lists, and memory concepts. Focus on implementation and tradeoffs.
// Array vs Linked List
Array (Static):
- Access: O(1) direct indexing arr[i]
- Insert/Delete: O(n) requires shifting
- Memory: Contiguous block, cache-friendly
- Use: When frequent access needed
Linked List (Dynamic):
- Access: O(n) must traverse
- Insert/Delete: O(1) if position known, else O(n)
- Memory: Scattered, pointer overhead (8-16 bytes per node)
- Use: When frequent insertions/deletions needed
// Stack (LIFO)
Array-based: O(1) push, pop; O(n) space
Operations: push(x), pop(), peek(), isEmpty()
Use: Function calls, undo-redo, bracket matching
// Queue (FIFO)
Array-based (circular): O(1) enqueue, dequeue
Operations: enqueue(x), dequeue(), front(), isEmpty()
Use: BFS, task scheduling, printer queue
// Deque (Double-ended)
Allows insertion/deletion from both ends: O(1)
Use: Sliding window problems, A* algorithm