iExamWorld
Notes

Data Structures and Algorithms: Complete Revision Notes for Placement Tests

English · Free on iExamWorld

Why DSA matters in placement tests

The technical sections of TCS NQT, Accenture, Capgemini, Cognizant, Wipro, Infosys and AMCAT ask short, trace-based questions: the output of a stack sequence, a traversal order, a hash slot or a sorting complexity. Practise tracing small inputs by hand, as the examples below do.

Arrays and linked lists

An array stores elements in contiguous memory, so any element can be reached by index in O(1) time. Inserting or deleting in the middle needs shifting, which costs O(n). A linked list chains nodes by pointers: access by position is O(n), but inserting at the head is O(1).

  • Singly linked list: each node points to the next node; the last node points to NULL.
  • Doubly linked list: each node has next and previous pointers, so it can be traversed both ways and a given node can be deleted in O(1) without searching for its predecessor.
  • Circular linked list: the last node points back to the first node instead of NULL. It suits round-robin scheduling.

Stacks

A stack follows LIFO (last in, first out). push adds to the top and pop removes from the top; both are O(1). Example: push 5, push 8, pop, push 3, push 9, pop, pop, push 7 leaves 5 and 7 in the stack, with 7 on top. Popping an empty stack is underflow. Uses include recursion, undo and bracket matching.

Infix to postfix

Scan the infix expression from left to right. Send operands straight to the output. Push an operator only after popping any operators on the stack that have higher precedence, or equal precedence when the operator is left-associative. Push a left bracket; on a right bracket pop until the matching left bracket. Pop everything left at the end.

InfixPostfix
A + B * C - D / EA B C * + D E / -
( A + B ) * ( C - D )A B + C D - *

Evaluating postfix

Push operands. On an operator, pop two values: the first popped is the right operand and the second is the left operand. Push the result. For 4 5 2 * + 9 3 / -, we compute 5 * 2 = 10, 4 + 10 = 14, 9 / 3 = 3 and finally 14 - 3 = 11.

Queues, circular queues, deques and priority queues

A queue follows FIFO (first in, first out): insert at the rear (enqueue) and remove from the front (dequeue). In a simple array queue, space freed at the front cannot be reused. A circular queue fixes this by wrapping indices: the next rear is (rear + 1) mod capacity. With capacity 6 and rear at 5, the next insertion goes to index 0. A common convention treats the queue as full when (rear + 1) mod capacity equals front.

  • Deque (double-ended queue): insertion and deletion at both ends.
  • Priority queue: each element has a priority and the highest-priority element (or the smallest key in a min-priority queue) leaves first. A binary heap gives O(log n) insert and delete.

Trees

A binary tree node has at most two children. Learn the three depth-first traversals by where the root is visited:

  • Inorder: left, root, right.
  • Preorder: root, left, right.
  • Postorder: left, right, root.

Example tree: root A; A has left child B and right child C; B has children D (left) and E (right); C has only a right child F. Then inorder is D B E A C F, preorder is A B D E C F and postorder is D E B F C A.

Height, full and complete trees

Height is usually the number of edges on the longest root-to-leaf path, but some books count nodes, so read the question carefully. A binary tree of height h (in edges) has at most 2^(h+1) - 1 nodes.

  • Full (strict) binary tree: every node has 0 or 2 children. Leaves = internal nodes + 1.
  • Complete binary tree: every level is full except possibly the last, which is filled from the left. Heaps are complete trees, which is why they fit neatly in an array.

Binary search tree (BST)

In a BST, keys in the left subtree are smaller than the node and keys in the right subtree are larger. Inserting 50, 30, 70, 20, 40, 60, 80, 35 places 35 to the left of 40, giving a height of 3 edges. The inorder traversal of a BST is always sorted. Operations take O(h): O(log n) when balanced, O(n) for sorted input that forms a chain.

Heaps

In a min-heap every parent is less than or equal to its children. In an array from index 0, the children of i are 2i + 1 and 2i + 2 and the parent is (i - 1) / 2 (integer division). To insert, add at the end and swap upward while smaller than the parent. Inserting 10, 4, 15, 2, 8 gives the array 2, 4, 15, 10, 8. Insert and delete-min are O(log n).

Graphs

A graph is a set of vertices joined by edges, which may be directed or undirected and weighted or unweighted. It is stored as an adjacency matrix (O(V^2) space, O(1) edge check) or an adjacency list (O(V + E) space, better for sparse graphs).

  • BFS uses a queue and visits vertices level by level.
  • DFS uses a stack or recursion and goes as deep as possible before backtracking.

For the directed lists A: B, C; B: D, E; C: F; E: F (D and F have none), BFS from A gives A B C D E F and DFS gives A B D E F C. Both run in O(V + E) with adjacency lists.

Shortest path: in an unweighted graph, BFS gives the fewest-edge path. With non-negative weights, Dijkstra's algorithm, which is greedy and uses a priority queue, finds single-source shortest paths. Bellman-Ford handles negative edge weights.

Hashing

A hash function maps a key to a table slot, for example h(k) = k mod m. A collision happens when two keys map to the same slot. Chaining keeps a linked list at each slot. Open addressing looks for another slot: linear probing tries h, h + 1, h + 2 and so on (wrapping round), while quadratic probing and double hashing use other step patterns. Example: with m = 7 and linear probing, 15, 22, 8 and 29 all hash to slot 1, so they land in slots 1, 2, 3 and 4. Linear probing suffers from primary clustering.

Sorting and searching

AlgorithmBestAverageWorstStableExtra space
Bubble sortO(n) with early-exit flagO(n^2)O(n^2)YesO(1)
Selection sortO(n^2)O(n^2)O(n^2)NoO(1)
Insertion sortO(n)O(n^2)O(n^2)YesO(1)
Merge sortO(n log n)O(n log n)O(n log n)YesO(n)
Quick sortO(n log n)O(n log n)O(n^2)NoO(log n) stack on average
Heap sortO(n log n)O(n log n)O(n log n)NoO(1)

A stable sort keeps equal keys in their original relative order. One pass of bubble sort on 5, 1, 4, 2, 8 gives 1, 4, 2, 5, 8, so the largest element reaches the end after the first pass. Insertion sort suits small or nearly sorted data. Quick sort hits its O(n^2) worst case when the pivot is always the smallest or largest element, for example a first-element pivot on already sorted input.

Linear search is O(n) and works on unsorted data. Binary search needs sorted data and is O(log n); it needs at most floor(log2 n) + 1 comparisons, which is 10 for 1,000 elements. Searching 27 in 3, 7, 11, 15, 19, 23, 27, 31, 35 compares 19 first and then finds 27.

Recursion, dynamic programming and greedy methods

A recursive function needs a base case and a step that moves towards it; otherwise it overflows the stack. Naive recursive Fibonacci repeats the same work and takes exponential time: fib(5) makes 15 calls. Memoisation stores each result the first time it is computed (top-down), and tabulation fills a table from the smallest case upwards (bottom-up). Both make Fibonacci O(n).

Dynamic programming applies when a problem has overlapping subproblems and optimal substructure. In the 0/1 knapsack problem each item is taken whole or not at all, and DP solves it in O(nW) time for n items and capacity W. With capacity 7 and items (1, 1), (3, 4), (4, 5), (5, 7), the best value is 9, from the items of weight 3 and 4.

A greedy algorithm takes the locally best choice at each step. It is optimal for fractional knapsack, activity selection, Huffman coding and minimum spanning trees (Prim, Kruskal). It can fail for 0/1 knapsack: in the example above, greedy by ratio picks weights 5 and 1 for a value of only 8.

Big-O comparison

Big-O ignores constants and lower-order terms: 3n^2 + 5n + 7 is O(n^2). The common order, from slowest to fastest growing, is O(1), O(log n), O(n), O(n log n), O(n^2), O(n^3), O(2^n), O(n!).

Quick revision points

  • Array access by index is O(1); linked list access by position is O(n).
  • In a circular linked list, the last node points to the head.
  • Stack is LIFO, queue is FIFO, and a deque allows operations at both ends.
  • Circular queue next index: (rear + 1) mod capacity.
  • Postfix evaluation: the first value popped is the right operand.
  • Inorder is L-Root-R, preorder is Root-L-R and postorder is L-R-Root.
  • The inorder traversal of a BST is sorted; a skewed BST degrades to O(n).
  • Full binary tree: leaves = internal nodes + 1.
  • Heap array: children at 2i + 1 and 2i + 2, parent at (i - 1) / 2.
  • BFS uses a queue and DFS uses a stack; both are O(V + E).
  • BFS gives shortest paths in unweighted graphs; Dijkstra needs non-negative weights.
  • Linear probing tries the next slot and causes primary clustering.
  • Merge sort is stable and O(n log n) in every case; quick sort's worst case is O(n^2).
  • Selection, quick and heap sort are not stable.
  • Memoisation turns exponential Fibonacci into O(n); greedy fails for 0/1 knapsack.

These notes were written with AI assistance for iExamWorld, based on the official syllabus. Spotted a mistake? Tell us.