Sub topic 18
GATE Problem Patterns and a Structure Comparison Table
Assume you know loops, references or pointers, basic recursion, and introductory Big-O notation. This chapter shows how to recognize common GATE patterns and select the correct data structure quickly.
A structure-selection checklist
Read the operation being optimized before looking at the data values.
- Need constant-time indexed access? Choose an array.
- Need insertion or deletion at a known node, with its predecessor available? Choose a linked list.
- Need the most recently added item first? Use a stack.
- Need the earliest added item first? Use a queue.
- Need the smallest or largest priority repeatedly? Use a priority queue, commonly implemented with a binary heap.
- Need sorted output from arbitrary values? Consider heap sort, but compare its costs with sorting alternatives.
The position of an operation matters. Inserting at the beginning of a linked list is \(O(1)\), but finding the insertion position may be \(O(n)\). Similarly, deleting an array element may require shifting many elements.
Common GATE patterns
Expression processing with a stack
Balanced-parentheses questions require matching each closing symbol with the most recent unmatched opening symbol. This is a last-in, first-out rule. Push opening symbols. For a closing symbol, compare it with the stack top. The expression is valid only if every comparison succeeds and the stack is empty at the end.
Infix-to-postfix conversion also uses a stack. Operands go directly to the output. Operators wait on the stack until operators with higher or equal precedence have been handled. Parentheses control when operators are removed.
Circular queue arithmetic
For an array of capacity \(C\), a circular queue wraps an index with modulo arithmetic:
\[ \text{next}(i)=(i+1)\bmod C \]
A queue that stores front and size is easy to analyze:
- Empty:
size == 0 - Full:
size == C - Rear position:
(front + size) % C
Do not confuse the full condition with rear == front. That condition is ambiguous when the queue stores only two indices.
Linked-list pointer tracing
To reverse a singly linked list, preserve the next node before changing the current node's pointer. Then move the previous and current references forward. The procedure takes \(O(n)\) time and \(O(1)\) extra space.
For cycle detection, Floyd's method moves one pointer by one node and another by two nodes. If they meet, a cycle exists. If the faster pointer reaches None, no cycle exists.
Heap questions
With zero-based indexing, a node at index \(i\) has:
- Parent: \((i-1)//2\), when \(i>0\)
- Left child: \(2i+1\)
- Right child: \(2i+2\)
Insertion and extraction take \(O(\log n)\). Bottom-up heap construction takes \(O(n)\), because most nodes are near the leaves and move only a short distance. Heap sort performs \(n\) logarithmic extractions, so its total time is \(O(n\log n)\).
Comparison table
| Structure | Access | Search | Insert | Delete | Typical use |
|---|---|---|---|---|---|
| Array | \(O(1)\) by index | \(O(n)\) | \(O(n)\) in the middle | \(O(n)\) in the middle | Indexed data |
| Singly linked list | \(O(n)\) | \(O(n)\) | \(O(1)\) at known position | \(O(1)\) with predecessor | Frequent local updates |
| Stack | Top \(O(1)\) | Not typical | Push \(O(1)\) | Pop \(O(1)\) | Parsing and recursion |
| Queue | Ends \(O(1)\) | Not typical | Enqueue \(O(1)\) | Dequeue \(O(1)\) | Scheduling |
| Binary heap | Root \(O(1)\) | \(O(n)\) generally | \(O(\log n)\) | Root \(O(\log n)\) | Priority selection |
Exercise
A circular queue has capacity 8, front = 6, and size = 5. Which array index contains the rear element?
Show answer
The rear index is (front + size - 1) % 8 = (6 + 5 - 1) % 8 = 2.