Welcome to Edugrown – Your Learning Partner EduGrown
🕑 Aaj 1 free content aur bacha hai. Login karein →
← Back to UG & PG Content
Exam Papers

Btech 3rd Sem Data Structures Solved Paper December 2023 (RGPV CS-303)

RGPV University (RGPV) · MP · Engineering (B.Tech / B.E.) · Computer science · 2nd Year · Semester 3 · 5 views

Advertisement

Btech 3rd Sem — Data Structures Solved Paper

RGPV Bhopal · CS/CT/CO-303 (GS) · Examination: December 2023
B.Tech III SemesterGrading System (GS)Time: 3 HoursMax Marks: 70
Exam instructions:
  • Attempt any five questions. All questions carry equal marks (14 each).
  • Marks of every part are shown beside the question; answers are written to that length: definition → working/algorithm → example/diagram → conclusion.
  • In case of any doubt, the English version of the question is final.

Question-wise topics

Q.No.Topics asked
Q1Loop complexity; ADT and 3-D array address (row/column major); Prim's algorithm
Q2Doubly linked list reversal function; infix → postfix; postfix evaluation
Q3Queue enqueue/dequeue and linked implementation; AVL insertion (10 keys)
Q4Binary tree from inorder + preorder; complexity of matrix multiplication
Q5Quick sort; O(n log n) proofs; binary search for 123
Q6Threaded binary tree, B-tree; adjacency list / matrix / multilist
Q7Tower of Hanoi; DFS and BFS algorithms
Q8Hashing — separate chaining, linear probing, double hashing
Question 1
Q.1 (a)4+4 Marks
What is the worst-case complexity of each of the following code fragments? (i) Two loops in a row: for (i = 0; i < N; i++) { sequence of statements } for (j = 0; j < M; j++) { sequence of statements } — How would the complexity change if the second loop went to N instead of M? (ii) A nested loop followed by a non-nested loop: for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { sequence of statements } } for (k = 0; k < M; k++) { sequence of statements }
Answer

Assume “sequence of statements” is a constant amount of work, O(1).

(i) Two loops in a row

First loop executes N times → N × O(1) = O(N)
Second loop executes M times → O(M)
Sequential statements add: T = O(N) + O(M)
Worst-case complexity = O(N + M). If the second loop also runs to N: O(N) + O(N) = O(2N) = O(N) (constant factor 2 is dropped) — linear.

(ii) Nested loop followed by a non-nested loop

Outer loop N times × inner loop N times → N × N = N² executions → O(N²)
Following loop runs M times → O(M)
T = O(N²) + O(M)
Worst-case complexity = O(N² + M). If M ≤ N² (e.g. M = N) the N² term dominates → O(N²); the result is O(M) only if M grows faster than N².

Rules used: consecutive statements → add complexities; nested loops → multiply; keep only the dominant term.

Q.1 (b)4 Marks
What is ADT? Compute the addresses of a 3-D array in a row and column major form.
Answer
An Abstract Data Type is a data type defined by its values and operations, independent of implementation (e.g. Stack with push/pop, Queue with enqueue/dequeue).

Let A[l₁…u₁][l₂…u₂][l₃…u₃] have dimensions n₁ = u₁ − l₁ + 1, n₂ = u₂ − l₂ + 1, n₃ = u₃ − l₃ + 1, base address B and element size W bytes.

Row-major: LOC(A[i][j][k]) = B + W·[(i − l₁)·n₂·n₃ + (j − l₂)·n₃ + (k − l₃)]
Column-major: LOC(A[i][j][k]) = B + W·[(k − l₃)·n₁·n₂ + (j − l₂)·n₁ + (i − l₁)]

In row-major order the last subscript varies fastest; in column-major order the first subscript varies fastest.

Example: int A[2][3][4] (0-based), B = 1000, W = 4. Address of A[1][0][2]:

  • Row-major: 1000 + 4·(1·3·4 + 0·4 + 2) = 1000 + 4·14 = 1056
  • Column-major: 1000 + 4·(2·2·3 + 0·2 + 1) = 1000 + 4·13 = 1052
Q.1 (c)2 Marks
Write Prim's algorithm to get minimal spanning tree out of a graph.
Answer
  1. Choose any start vertex s; T = {s}, MST edge set = ∅.
  2. While T does not contain all vertices: select the minimum-weight edge (u, v) with u ∈ T and v ∉ T.
  3. Add v to T and (u, v) to the MST.
  4. Stop when V − 1 edges are selected.

Complexity O(V²) with adjacency matrix, O(E log V) with a min-heap.

Question 2
Q.2 (a)4 Marks
Consider the following function that takes reference to head of a doubly linked list as parameter (node has prev and next pointers): void fun(struct node **head_ref) { struct node *temp = NULL; struct node *current = *head_ref; while (current != NULL) { temp = current->prev; current->prev = current->next; current->next = temp; current = current->prev; } if (temp != NULL) *head_ref = temp->prev; } If the list 1 <--> 2 <--> 3 <--> 4 <--> 5 <--> 6 is passed, what should be the modified linked list after the function call?
Answer

For every node the loop swaps its prev and next pointers, then moves to the next original node (which is now stored in current→prev). Swapping both pointers of every node reverses the list.

currenttemp = old prevafter swap: prev / nextcurrent moves to
1NULLprev = 2, next = NULL2
21prev = 3, next = 13
32prev = 4, next = 24
43prev = 5, next = 35
54prev = 6, next = 46
65prev = NULL, next = 5NULL (stop)

After the loop temp = node 5, and temp→prev is now node 6, so *head_ref = node 6.

HEAD654321NULL
Doubly linked list after the call
The function reverses the doubly linked list: 6 <--> 5 <--> 4 <--> 3 <--> 2 <--> 1 (head now points to 6).
Q.2 (b)5+5 Marks
(i) Transform the following expression in postfix notation: A + (B + D)/E − F*(G + H/K)   (ii) Evaluate the following postfix expression: ABC*D/+ where A = 2, B = 3, C = 4, D = 6
Answer

(i) Infix to postfix

Precedence: *, / > +, −; all left-associative. Scan left to right using an operator stack:

SymbolStackPostfix expressionAction
A—AOperand → output
++APush +
(+ (APush (
B+ (A BOperand → output
++ ( +A BPush +
D+ ( +A B DOperand → output
)+A B D +Pop till ( : +
/+ /A B D +Push /
E+ /A B D + EOperand → output
--A B D + E / +Pop / +, push -
F-A B D + E / + FOperand → output
*- *A B D + E / + FPush *
(- * (A B D + E / + FPush (
G- * (A B D + E / + F GOperand → output
+- * ( +A B D + E / + F GPush +
H- * ( +A B D + E / + F G HOperand → output
/- * ( + /A B D + E / + F G HPush /
K- * ( + /A B D + E / + F G H KOperand → output
)- *A B D + E / + F G H K / +Pop till ( : / +
(end)—A B D + E / + F G H K / + * -Pop remaining: * -
Postfix: A B D + E / + F G H K / + * -

(ii) Evaluation of ABC*D/+ (A = 2, B = 3, C = 4, D = 6) — i.e. 2 3 4 * 6 / +

Operand → push; operator → pop two values (second popped is the left operand), apply, push the result.

SymbolActionStack
APush 22
BPush 32 3
CPush 42 3 4
*Pop 4, 3; 3 * 4 = 12; push2 12
DPush 62 12 6
/Pop 6, 12; 12 / 6 = 2; push2 2
+Pop 2, 2; 2 + 2 = 4; push4
Value = 4   (= 2 + (3 × 4)/6 = 2 + 2)
Question 3
Q.3 (a)4+4 Marks
Write an algorithm to enqueue and dequeue an element in a queue. The queue implementation using a linked list?
Answer
A queue is a linear data structure in which insertion (ENQUEUE) takes place at the REAR and deletion (DEQUEUE) at the FRONT — FIFO order.

Algorithm ENQUEUE(QUEUE, MAX, FRONT, REAR, ITEM) — array

  1. If REAR = MAX − 1 → print “Overflow” and exit.
  2. If FRONT = −1 then FRONT = 0.
  3. REAR = REAR + 1.
  4. QUEUE[REAR] = ITEM.
  5. Exit.

Algorithm DEQUEUE(QUEUE, FRONT, REAR)

  1. If FRONT = −1 or FRONT > REAR → print “Underflow” and exit.
  2. ITEM = QUEUE[FRONT].
  3. If FRONT = REAR then FRONT = REAR = −1 (queue becomes empty) else FRONT = FRONT + 1.
  4. Return ITEM.
100201302345frontrearQueue: 10 leaves first
After enqueue 10, 20, 30

Queue implementation using a linked list

Two pointers are kept: front (first node, for deletion) and rear (last node, for insertion). Both operations are O(1) and the queue never becomes full until memory is exhausted.

FRONT102030NULL
front → 10 → 20 → 30 ← rear
  • Enqueue: create node t; t→next = NULL; if rear = NULL then front = rear = t else rear→next = t, rear = t.
  • Dequeue: if front = NULL → underflow; t = front; front = front→next; if front = NULL then rear = NULL; free(t).
#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};
struct node *front = NULL, *rear = NULL;

void enqueue(int x) {
    struct node *t = (struct node *)malloc(sizeof(struct node));
    t->data = x;
    t->next = NULL;
    if (rear == NULL)            /* queue empty */
        front = rear = t;
    else {
        rear->next = t;
        rear = t;
    }
}

int dequeue(void) {
    if (front == NULL) { printf("Queue Underflow\n"); return -1; }
    struct node *t = front;
    int x = t->data;
    front = front->next;
    if (front == NULL) rear = NULL;   /* queue became empty */
    free(t);
    return x;
}

void display(void) {
    struct node *p = front;
    printf("Queue: ");
    while (p != NULL) { printf("%d ", p->data); p = p->next; }
    printf("\n");
}

int main(void) {
    enqueue(10); enqueue(20); enqueue(30);
    display();
    printf("Dequeued %d\n", dequeue());
    display();
    return 0;
}

Output: Queue: 10 20 30 → Dequeued 10 → Queue: 20 30.

Q.3 (b)6 Marks
Insert the following data keys in the AVL Tree: 16, 23, 9, 163, 64, 29, 73, 83, 90, 96 (10 keys).
Answer

Insert as in a BST; after each insertion compute BF = h(L) − h(R) on the path to the root and apply the rotation for the first node with |BF| = 2.

16Insert 16
Tree remains balanced — no rotation.
1623Insert 23
Tree remains balanced — no rotation.
91623Insert 9
Tree remains balanced — no rotation.
91623163Insert 163
Tree remains balanced — no rotation.
9162364163Insert 64
Node 23 has BF = -2, its right child 163 is left-heavy → RL case: right rotation at 163, then left rotation at 23.
916232964163Insert 29
Node 16 has BF = -2, its right child 64 is left-heavy → RL case: right rotation at 64, then left rotation at 16.
91623296473163Insert 73
Tree remains balanced — no rotation.
9162329647383163Insert 83
Node 163 has BF = +2, its left child 73 is right-heavy → LR case: left rotation at 73, then right rotation at 163.
916232964738390163Insert 90
Node 64 has BF = -2 (right-right heavy) → RR case: single left rotation at 64.
91623296473839096163Insert 96
Node 163 has BF = +2, its left child 90 is right-heavy → LR case: left rotation at 90, then right rotation at 163.
91623296473839096163
Final AVL tree
Final AVL tree — preorder: 23, 16, 9, 83, 64, 29, 73, 96, 90, 163; inorder: 9, 16, 23, 29, 64, 73, 83, 90, 96, 163. Rotations used: RL at 23, RL at 16, LR at 163, RR at 64, LR at 163.
Question 4
Q.4 (a)4+2 Marks
The in-order and pre-order traversal sequence of a node in a binary tree are given below: In-order: E A C K F H D B G; Pre-order: F A E K C D H G B. Draw the binary tree. State briefly the logic used to construct the tree.
Answer

Logic

  1. The first element of the preorder sequence is the root.
  2. Locate the root in the inorder sequence: elements to its left form the left subtree, elements to its right form the right subtree.
  3. Take the next elements of the preorder (as many as the size of each part) and repeat recursively for the left and right subtrees.

Construction

StepPreorder partInorder partRootLeft | Right (inorder)
1F A E K C D H G BE A C K F H D B GFE A C K | H D B G
2A E K CE A C KAE | C K
3K CC KKC | —
4D H G BH D B GDH | B G
5G BB GGB | —
EACKFHDBG
Constructed binary tree

Check: preorder of the tree = F A E K C D H G B ✔, inorder = E A C K F H D B G ✔, postorder = E C K A H B G D F.

Root F; left subtree A (left E, right K → left C); right subtree D (left H, right G → left B).
Q.4 (b)2×4=8 Marks
Describe the performance of the algorithms used to multiply two N × N matrices using suitable measures of complexity. You should make clear what operations you are counting, what is the worst-case that you are considering (and, perhaps, average-case and best-case, where appropriate). Also consider space complexity.
Answer

1. Standard (iterative) algorithm

for (i = 0; i < N; i++)
    for (j = 0; j < N; j++) {
        C[i][j] = 0;
        for (k = 0; k < N; k++)
            C[i][j] = C[i][j] + A[i][k] * B[k][j];
    }

Operations counted: scalar multiplications and additions (the basic operations of the innermost statement).

  • Multiplications: N · N · N = N³.
  • Additions: N³ (or N²(N − 1) if C[i][j] is initialised with the first product).
  • Assignments/comparisons of loop counters are proportional to these, so T(N) = Θ(N³).

Best, average and worst case are the same, Θ(N³): the loops always run their full ranges — there is no data-dependent branch, so the values in A and B do not affect the count. (Only special matrices — sparse, diagonal — allow faster special algorithms.)

Space: input matrices 2N², output N² → total Θ(N²); auxiliary space O(1) (only i, j, k).

2. Divide-and-conquer and Strassen's algorithm

  • Split each matrix into four N/2 × N/2 blocks: simple divide and conquer needs 8 block multiplications → T(N) = 8T(N/2) + Θ(N²) = Θ(N³) (no gain).
  • Strassen uses only 7 block multiplications (and 18 additions) → T(N) = 7T(N/2) + Θ(N²) = Θ(Nlog₂7) ≈ Θ(N2.81) in all cases.
  • Strassen needs Θ(N²) extra space for temporary sub-matrices and its recursion stack is O(log N) deep; it pays off only for large N.
AlgorithmBest = Average = WorstExtra space
Standard triple loopΘ(N³)O(1)
Divide & conquer (8 products)Θ(N³)O(N²)
Strassen (7 products)Θ(N^2.81)O(N²)
Counting scalar multiplications, classical matrix multiplication is Θ(N³) time in the best, average and worst case, with Θ(N²) total / O(1) auxiliary space.
Question 5
Q.5 (a)5+4 Marks
(i) Sort the following data in ascending order using Quick sort: 9, 4, 12, 6, 5, 10, 7.   (ii) Prove that Heap sort, Merge sort, Quick sort takes O(n log n) time in the worst case?
Answer

(i) Quick sort

Partition (first element as pivot): move i right while a[i] ≤ pivot, move j left while a[j] > pivot; if i < j swap a[i] and a[j]; when they cross, swap the pivot with a[j]. The pivot is then in its final place; sort the left and right parts recursively.

Sub-array (index)PivotSwaps during partitionArray after partition
[0..6]9swap 12 ↔ 7; j crossed i → swap pivot 9 with 5547691012
[0..3]5j crossed i → swap pivot 5 with 4457691012
[2..3]7j crossed i → swap pivot 7 with 6456791012
[5..6]10pivot 10 stays456791012

Pivot shown in red. First partition in detail: pivot 9, i stops at 12, j stops at 7 → swap → [9, 4, 7, 6, 5, 10, 12]; i stops at 10, j stops at 5 → crossed → swap 9 with 5 → [5, 4, 7, 6, 9, 10, 12].

Sorted: 4, 5, 6, 7, 9, 10, 12

(ii) Worst-case running times

Heap sort: Build-heap takes O(n). Then n − 1 times: swap root with last element and heapify the root — each heapify moves down at most the height ⌊log₂ n⌋. Total ≤ O(n) + (n − 1)·O(log n) = O(n log n) in every case.

Merge sort: T(n) = 2T(n/2) + cn (divide in two halves, merge in linear time). The recursion tree has log₂ n levels, each level costs cn in total ⇒ T(n) = cn log₂ n + cn = O(n log n) for every input.

Quick sort: if every partition splits the array evenly, T(n) = 2T(n/2) + cn = O(n log n) (best and average case). But in the worst case (e.g. already sorted data with first-element pivot) one part is empty: T(n) = T(n − 1) + cn = c(n + (n − 1) + … + 1) = O(n²).

So the statement holds for heap sort and merge sort; quick sort is O(n log n) only on average. Its worst case becomes O(n log n) only if the pivot is chosen as the true median (median-of-medians), or with high probability using a random pivot.
Q.5 (b)5 Marks
Apply Binary Search to find 123 in the following list: 49, 98, 101, 123, 149, 194, 199, 211, 240, 286, 840, 930 (12 data).
Answer

The list is already sorted (indices 0–11). low = 0, high = 11.

lowhighmida[mid]Decision
0115194123 < 194 → search left half: high = 4
042101123 > 101 → search right half: low = 3
343123Found
123 is found at index 3 (4th position) after 3 comparisons (log₂ 12 ≈ 3.6, so at most 4 are ever needed).
Question 6
Q.6 (a)2+3 Marks
Is there any difference between threaded and unthreaded binary tree? What is a B-tree? Explain.
Answer

Yes, there is a difference. In an ordinary (unthreaded) binary tree with n nodes there are 2n link fields, of which n + 1 are NULL — wasted space. A threaded binary tree replaces these NULL links with pointers called threads: a NULL right link points to the node's inorder successor and a NULL left link to its inorder predecessor. One extra bit per link (lthread/rthread) tells whether it is a child pointer or a thread.

ABCDE
Inorder A B C D E: in the threaded version A→right thread → B, C→right thread → D, C→left thread → B
Unthreaded binary treeThreaded binary tree
NULL pointers in leaf links (n + 1 of them)NULL links replaced by threads to inorder predecessor/successor
Inorder traversal needs recursion or an explicit stackInorder traversal without stack or recursion
Finding inorder successor needs a search from rootSuccessor found directly via thread (O(1) in many cases)
Simpler insertion/deletionInsertion/deletion must also update threads; needs flag bits

B-tree

A B-tree of order m is a balanced m-way search tree used for disk-based storage (databases, file systems) in which:
1. every node has at most m children and m − 1 keys (kept in sorted order);
2. every node except the root has at least ⌈m/2⌉ children (⌈m/2⌉ − 1 keys);
3. the root has at least 2 children if it is not a leaf;
4. all leaves are at the same level;
5. keys in a node separate the keys of its subtrees (like a BST).
3060102040507080
A B-tree of order 3 (2–3 tree)
  • Insertion: insert into the proper leaf; if it overflows (m keys), split it — the median key moves up to the parent (splits may propagate to the root, increasing height).
  • Deletion: remove the key; if a node underflows, borrow from a sibling or merge with it.
  • Advantages: height is O(logm n) — very few disk accesses because each node holds many keys (one node = one disk block).
Q.6 (b)3×3=9 Marks
For the graph shown below find the following: (i) Adjacency list representation (ii) Adjacency matrix representation (iii) Adjacency multi list representation
Answer
ABCDEF
Edges: A–B, A–C, A–D, A–E, B–D, B–E, C–D, D–F, E–F (undirected, 6 vertices, 9 edges)

(i) Adjacency list

An array of head pointers, one per vertex; each points to a linked list of that vertex's neighbours (total 2E = 18 list nodes).

VertexAdjacency list
AB → C → D → E → NULL
BA → D → E → NULL
CA → D → NULL
DA → B → C → F → NULL
EA → B → F → NULL
FD → E → NULL

(ii) Adjacency matrix

6 × 6 matrix, a[i][j] = 1 if vertices i and j are adjacent, else 0. It is symmetric (undirected graph); row sums give the degrees.

ABCDEF
A011110
B100110
C100100
D111001
E110001
F000110

Degrees: A = 4, B = 3, C = 2, D = 4, E = 3, F = 2 (sum 18 = 2 × 9 edges).

(iii) Adjacency multi-list

Each edge is stored only once as a node with fields [mark | vi | vj | linki | linkj], where linki points to the next edge node incident on vi and linkj to the next edge node incident on vj. Each vertex has a head pointer to its first edge.

Edge nodemarkvivjlink for vilink for vj
N10ABN2N5
N20ACN3N7
N30ADN4N5
N40AENULLN6
N50BDN6N7
N60BENULLN9
N70CDNULLN8
N80DFNULLN9
N90EFNULLNULL
VertexHead → first edgeEdges reached by following links
AN1N1 → N2 → N3 → N4
BN1N1 → N5 → N6
CN2N2 → N7
DN3N3 → N5 → N7 → N8
EN4N4 → N6 → N9
FN8N8 → N9
Adjacency matrix uses Θ(V²) space; adjacency list Θ(V + 2E); the multilist stores each edge once (9 nodes) and lets an edge be marked as visited from either end.
Question 7
Q.7 (a)2+5 Marks
What do you mean by Tower of Hanoi problem? Explain with suitable example along with diagram?
Answer
Tower of Hanoi: there are three pegs A (source), B (auxiliary), C (destination) and n disks of different sizes stacked on A in decreasing size (largest at bottom). Move all disks to C obeying the rules: (1) only one disk is moved at a time; (2) only the top disk of a peg can be moved; (3) a larger disk can never be placed on a smaller disk.
Peg A (source)Peg B (auxiliary)Peg C (destination)Disk 3Disk 2Disk 1
Initial position for n = 3

Recursive solution

void TOH(int n, char src, char aux, char dest) {
    if (n == 1) {
        printf("Move disk 1 from %c to %c\n", src, dest);
        return;
    }
    TOH(n - 1, src, dest, aux);        /* move n-1 disks to auxiliary */
    printf("Move disk %d from %c to %c\n", n, src, dest);
    TOH(n - 1, aux, src, dest);        /* move n-1 disks onto disk n */
}

Example: n = 3 — TOH(3, A, B, C)

MoveDiskFrom → To
11A → C
22A → B
31C → B
43A → C
51B → A
62B → C
71A → C

Recurrence: T(n) = 2T(n − 1) + 1, T(1) = 1 ⇒ T(n) = 2ⁿ − 1 moves (7 for n = 3, 15 for n = 4); time O(2ⁿ), recursion depth O(n).

All 3 disks reach peg C in the minimum 7 moves.
Q.7 (b)3.5+3.5 Marks
Write algorithms for DFS and BFS traversal on a graph.
Answer

Breadth First Search (BFS)

Visits all neighbours of a vertex first, then their neighbours — level by level. Uses a queue.

  1. Mark all vertices unvisited. Enqueue the start vertex S and mark it visited.
  2. While the queue is not empty: dequeue a vertex U and process (print) it.
  3. For every neighbour V of U that is unvisited: mark V visited and enqueue V.
  4. Stop when the queue is empty.

Depth First Search (DFS)

Goes as deep as possible along a path before backtracking. Uses a stack (or recursion).

  1. Mark all vertices unvisited; call DFS(S).
  2. DFS(U): mark U visited and process it.
  3. For every neighbour V of U: if V is unvisited, call DFS(V) (push on stack).
  4. When U has no unvisited neighbour, return (pop / backtrack).
void BFS(int s) {
    int u, v;
    enqueue(s);  visited[s] = 1;
    while (!isEmpty()) {
        u = dequeue();
        printf("%d ", u);
        for (v = 0; v < n; v++)
            if (adj[u][v] == 1 && visited[v] == 0) {
                visited[v] = 1;
                enqueue(v);
            }
    }
}

void DFS(int u) {
    int v;
    visited[u] = 1;
    printf("%d ", u);
    for (v = 0; v < n; v++)
        if (adj[u][v] == 1 && visited[v] == 0)
            DFS(v);
}

Example

ABCDEF
DequeuedNewly enqueuedQueueBFS order
AB, CB CA
BD, EC D EA B
CFD E FA B C
D—E FA B C D
E—FA B C D E
F—emptyA B C D E F
BFS from A: A → B → C → D → E → F    DFS from A: A → B → D → E → F → C

DFS trace: A → B → D (dead end, backtrack to B) → E → F → C (all visited, backtrack to A). Both take O(V + E) time with adjacency lists (O(V²) with a matrix).

Question 8
Q.84+5+5 Marks
Given the values {2341, 4234, 2839, 430, 22, 397, 3920}, a hash table of size 7 and a hash function h(x) = x mod 7, show the resulting table after inserting the values in the given order with each of the following collision strategies: (i) separate chaining (ii) linear probing (iii) double hashing with second hash function h₁(x) = (2x − 1) mod 7
Answer

Hash values

xh(x) = x mod 7h₁(x) = (2x − 1) mod 7
23412341 mod 7 = 3(2·2341 − 1) mod 7 = 5
42344234 mod 7 = 6(2·4234 − 1) mod 7 = 4
28392839 mod 7 = 4(2·2839 − 1) mod 7 = 0
430430 mod 7 = 3(2·430 − 1) mod 7 = 5
2222 mod 7 = 1(2·22 − 1) mod 7 = 1
397397 mod 7 = 5(2·397 − 1) mod 7 = 2
39203920 mod 7 = 0(2·3920 − 1) mod 7 = 6

Collisions: 2341 and 430 both hash to 3.

(i) Separate chaining

Each slot keeps a linked list; a colliding key is appended to the list of its slot.

IndexChain
03920 → NULL
122 → NULL
2NULL
32341 → 430 → NULL
42839 → NULL
5397 → NULL
64234 → NULL

(ii) Linear probing

On collision try h(x) + 1, h(x) + 2, … (mod 7) until a free slot is found.

KeySlots probedPlaced at
234133
423466
283944
4303 → 4 → 55
2211
3975 → 6 → 00
39200 → 1 → 22
Index0123456
Key397223920234128394304234

430 → 3 (taken by 2341) → 4 (2839) → 5 ✔; 397 → 5 (430) → 6 (4234) → 0 ✔; 3920 → 0 (397) → 1 (22) → 2 ✔ — note the clustering.

(iii) Double hashing

Probe sequence: indexk = (h(x) + k·h₁(x)) mod 7, k = 0, 1, 2, …

Keyh(x)h₁(x)Slots probedPlaced at
23413533
42346466
28394044
430353 → 11
22111 → 22
3975255
39200600
Index0123456
Key392043022234128393974234

430: h = 3 occupied, step 5 → (3 + 5) mod 7 = 1 ✔. 22: h = 1 now occupied by 430, step 1 → 2 ✔. 397 → 5 and 3920 → 0 directly.

A good second hash function must never be 0 (here h₁(2839) = 0, which would loop if 2839 had collided); usually h₂(x) = R − (x mod R) is used.
Chaining: 0:3920, 1:22, 3:2341→430, 4:2839, 5:397, 6:4234.   Linear probing: [397, 22, 3920, 2341, 2839, 430, 4234].   Double hashing: [3920, 430, 22, 2341, 2839, 397, 4234].
Exam tip: Write the definition first, show every step of the algorithm or working (pass tables, the tree after each step), draw neat diagrams and box the final answer — examiners award step marks.
Advertisement

💬 Comments & Doubts

💭

Abhi tak koi comment nahi

Sabse pehle comment karne wale bano!

Apna comment likhein

Doubt ho ya suggestion — kuch bhi poochh sakte hain. Login zaroori nahi hai.

7 + 10 =

Ye sirf ye confirm karne ke liye hai ki aap robot nahi hain.

Comment publish hone se pehle admin check karta hai.

🔒 Content copy nahi kar sakte