Skip to main content

Chapter 7

Data Structure and Algorithm with Java (old course)

TU BIM past exam questions

Past questions and answers

82 questions set from this chapter, 2 of them more than once. Most repeated first.

  • Asked 2 times
  • 2015 AD · 1 mark
  • 2016 AD · 1 mark

What is Binary tree?

Answer

A binary tree is a tree data structure in which each node has at most two children, called the left child and the right child. It may be empty. A binary tree with nn nodes has height at least ⌈log⁡2(n+1)⌉−1\lceil \log_2(n+1) \rceil - 1 and at most n−1n-1. It is used for expression trees, binary search trees and heaps.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2017 AD · 1 mark
  • 2019 AD · 1 mark

Define skip list.

Answer

A skip list is a probabilistic data structure built from several layers of sorted linked lists. The bottom layer contains all the elements, and each higher layer is a sparser "express lane" that skips over elements of the layer below. Search, insertion and deletion take O(log⁡n)O(\log n) on average.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 4 marks

Create heap tree from given data: 20, 22, 33, 44, 12, 15, 18, 19, 20.

Similar questions: Create heap tree from given data and (2017 AD)

Answer

Build a max-heap by inserting the keys one by one and sifting up (swap with the parent while larger).

InsertHeap array after insertion
2020
2222, 20 (swap with 20)
3333, 20, 22 (swap with 22)
4444, 33, 22, 20 (swaps with 20, then 33)
1244, 33, 22, 20, 12
1544, 33, 22, 20, 12, 15
1844, 33, 22, 20, 12, 15, 18
1944, 33, 22, 20, 12, 15, 18, 19
2044, 33, 22, 20, 12, 15, 18, 19, 20

Final heap array: [44, 33, 22, 20, 12, 15, 18, 19, 20]

                 44
             /        \
           33          22
         /    \       /   \
       20     12    15    18
      /  \
    19    20

Every parent is greater than or equal to its children, so this is a valid max-heap.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 4 marks

Write a java function to insert element into queue.

Similar questions: Write a Java function to insert element (2017 AD)

Answer

An array-based linear queue with front, rear and size/capacity fields:

class Queue {
    int[] arr;
    int front = 0, rear = -1, capacity;

    Queue(int capacity) {
        this.capacity = capacity;
        arr = new int[capacity];
    }

    void enqueue(int item) {
        if (rear == capacity - 1) {          // overflow check
            System.out.println("Queue is full (overflow)");
            return;
        }
        arr[++rear] = item;                   // insert at rear
        System.out.println(item + " inserted");
    }
}

The function first checks for overflow, then increments rear and stores the item there. Time complexity is O(1)O(1).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 4 marks

Write a Java function to insert element into queue.

Similar questions: Write a java function to insert element (2016 AD)

Answer

An array-based linear queue with front, rear and size/capacity fields:

class Queue {
    int[] arr;
    int front = 0, rear = -1, capacity;

    Queue(int capacity) {
        this.capacity = capacity;
        arr = new int[capacity];
    }

    void enqueue(int item) {
        if (rear == capacity - 1) {          // overflow check
            System.out.println("Queue is full (overflow)");
            return;
        }
        arr[++rear] = item;                   // insert at rear
        System.out.println(item + " inserted");
    }
}

The function first checks for overflow, then increments rear and stores the item there. Time complexity is O(1)O(1).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 4 marks

Create heap tree from given data and sort it using heap sort: 12, 23, 44, 56, 33, 67, 37.

Similar questions: Create heap tree from given data: 20 (2015 AD)

Answer

Build a max-heap from 12,23,44,56,33,67,3712, 23, 44, 56, 33, 67, 37 (array indices 1 to 7, children of ii are 2i2i and 2i+12i+1).

Initial array: [12,23,44,56,33,67,37][12, 23, 44, 56, 33, 67, 37]

  1. Heapify node 44 (children 67, 37): swap with 67 gives [12,23,67,56,33,44,37][12, 23, 67, 56, 33, 44, 37].
  2. Heapify node 23 (children 56, 33): swap with 56 gives [12,56,67,23,33,44,37][12, 56, 67, 23, 33, 44, 37].
  3. Heapify node 12 (children 56, 67): swap with 67 gives [67,56,12,23,33,44,37][67, 56, 12, 23, 33, 44, 37], then 12 swaps with 44 (children 44, 37), giving the max-heap:

[67,56,44,23,33,12,37][67, 56, 44, 23, 33, 12, 37]

        67
      /    \
    56      44
   /  \    /  \
  23  33  12  37

Heap sort: swap the root with the last element of the heap, reduce the heap size and re-heapify.

StepArray after swap and re-heapify (sorted part after bar)
1[56,37,44,23,33,12∣67][56, 37, 44, 23, 33, 12 \mid 67]
2[44,37,12,23,33∣56,67][44, 37, 12, 23, 33 \mid 56, 67]
3[37,33,12,23∣44,56,67][37, 33, 12, 23 \mid 44, 56, 67]
4[33,23,12∣37,44,56,67][33, 23, 12 \mid 37, 44, 56, 67]
5[23,12∣33,37,44,56,67][23, 12 \mid 33, 37, 44, 56, 67]
6[12∣23,33,37,44,56,67][12 \mid 23, 33, 37, 44, 56, 67]

Sorted result:

12,23,33,37,44,56,6712, 23, 33, 37, 44, 56, 67

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What is theta notation ?

Answer

Theta (Θ\Theta) notation gives a tight bound on the growth of a function. f(n)=Θ(g(n))f(n) = \Theta(g(n)) if there are positive constants c1,c2,n0c_1, c_2, n_0 such that

c1 g(n)≤f(n)≤c2 g(n)for all n≥n0c_1\, g(n) \le f(n) \le c_2\, g(n) \quad \text{for all } n \ge n_0

That is, f(n)f(n) is bounded both above (Big-O) and below (Ω\Omega) by g(n)g(n). For example, 3n2+2n=Θ(n2)3n^2 + 2n = \Theta(n^2).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What is a self adjusting data structure?

Answer

A self-adjusting data structure changes its own organisation automatically as it is accessed, so that frequently used elements become cheaper to reach. It needs no extra balance or count information. Examples are the splay tree (an accessed node is moved to the root by rotations) and the self-organising list (move-to-front, transpose, frequency count). It gives good amortised performance.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What is priority queue?

Answer

A priority queue is an abstract data type in which every element has a priority and the element with the highest priority (or lowest, in a min priority queue) is removed first, regardless of the order of insertion. Elements with equal priority are served in order of arrival. It is commonly implemented with a heap, giving O(log⁡n)O(\log n) insertion and deletion.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What do you mean by tail recursion?

Answer

A recursive function is tail recursive if the recursive call is the last operation it performs, so nothing remains to be done after the call returns. Such calls can be converted into a loop (tail-call optimisation), which saves stack space. Example:

int factTail(int n, int acc) {
    if (n <= 1) return acc;
    return factTail(n - 1, n * acc);
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Define expression tree.

Answer

An expression tree is a binary tree that represents an arithmetic expression. Each leaf holds an operand and each internal node holds an operator whose children are its operands. For example, (a+b)∗c(a+b)*c has * as root, with + (children a, b) as its left child and c as its right child. Its inorder traversal gives the infix, postorder the postfix and preorder the prefix form.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What do you mean by best case complexity of an algorithm?

Answer

The best-case complexity of an algorithm is the minimum amount of time (or space) it takes over all inputs of size nn, that is, the running time on the most favourable input. For example, linear search finds the key at the first position, so its best case is O(1)O(1); insertion sort on an already sorted array has best case O(n)O(n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What is a linear probing?

Answer

Linear probing is an open-addressing collision resolution method in hashing. If the slot h(k)h(k) is already occupied, the table is searched sequentially for the next free slot:

h(k,i)=(h(k)+i) mod m,i=0,1,2,…h(k, i) = (h(k) + i) \bmod m, \quad i = 0, 1, 2, \dots

It is simple but suffers from primary clustering. For example, with m=10m = 10 and h(k)=k mod 10h(k) = k \bmod 10, key 25 goes to slot 5, and key 35 (also hashing to 5) goes to slot 6.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What is topological sorting?

Answer

Topological sorting is a linear ordering of the vertices of a directed acyclic graph (DAG) such that for every edge u→vu \to v, vertex uu comes before vv in the ordering. It is possible only if the graph has no cycle. It can be found with DFS (reverse finishing order) or by repeatedly removing vertices of in-degree zero (Kahn's algorithm). It is used in task scheduling and in resolving course prerequisites.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What are the elementary sorting algorithms?

Answer

The elementary (simple) sorting algorithms, each taking O(n2)O(n^2) time in the average and worst case, are:

  1. Bubble sort: repeatedly swaps adjacent elements that are out of order.
  2. Selection sort: repeatedly selects the smallest remaining element and places it in position.
  3. Insertion sort: inserts each element into its correct place in the sorted part of the array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 4 marks

Write a function in Java to sort integers. (Use any algorithm)

Answer

Insertion sort in Java:

public static void insertionSort(int[] a) {
    for (int i = 1; i < a.length; i++) {
        int key = a[i];
        int j = i - 1;
        while (j >= 0 && a[j] > key) {
            a[j + 1] = a[j];   // shift larger element right
            j--;
        }
        a[j + 1] = key;
    }
}

Each element a[i] is inserted into its correct place in the already sorted part a[0..i-1]. Time complexity is O(n2)O(n^2) in the worst case and O(n)O(n) in the best case.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 4 marks

Write a program in Java to create a linear linked list

Answer

class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; }
}

class LinkedList {
    Node head;

    void insertEnd(int data) {
        Node n = new Node(data);
        if (head == null) { head = n; return; }
        Node t = head;
        while (t.next != null) t = t.next;
        t.next = n;
    }

    void display() {
        for (Node t = head; t != null; t = t.next)
            System.out.print(t.data + " -> ");
        System.out.println("null");
    }
}

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insertEnd(10);
        list.insertEnd(20);
        list.insertEnd(30);
        list.display();   // 10 -> 20 -> 30 -> null
    }
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 4 marks

Write a program in Java to create stack (push operation only).

Answer

class Stack {
    private int[] arr;
    private int top = -1;

    Stack(int size) { arr = new int[size]; }

    void push(int x) {
        if (top == arr.length - 1) {
            System.out.println("Stack overflow");
            return;
        }
        arr[++top] = x;
    }

    void display() {
        for (int i = top; i >= 0; i--)
            System.out.println(arr[i]);
    }
}

public class StackDemo {
    public static void main(String[] args) {
        Stack s = new Stack(5);
        s.push(10);
        s.push(20);
        s.push(30);
        s.display();   // 30 20 10 (top first)
    }
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 4 marks

Write a program in Java to store number in array using concept of hashing.

Answer

The program stores numbers in an array using the hash function h(k)=k mod mh(k) = k \bmod m, with linear probing to resolve collisions.

public class HashDemo {
    static final int SIZE = 10;
    static int[] table = new int[SIZE];

    static void insert(int key) {
        int idx = key % SIZE;
        int count = 0;
        while (table[idx] != -1 && count < SIZE) {   // collision: probe next slot
            idx = (idx + 1) % SIZE;
            count++;
        }
        if (count == SIZE) System.out.println("Table full");
        else table[idx] = key;
    }

    public static void main(String[] args) {
        java.util.Arrays.fill(table, -1);            // -1 means empty
        int[] data = {25, 36, 15, 45, 7};
        for (int k : data) insert(k);
        for (int i = 0; i < SIZE; i++)
            System.out.println(i + " : " + (table[i] == -1 ? "-" : table[i]));
    }
}

Here 25 goes to index 5, 36 to 6, 15 collides at 5 and goes to 7, 45 goes to 8 and 7 collides at 7 and goes to 9.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 5 marks

Define graph. Write Dijkstra's algorithm to find shortest path in graph with example.

Answer

Graph: a graph G=(V,E)G = (V, E) is a set of vertices VV and a set of edges EE connecting pairs of vertices. It may be directed or undirected, weighted or unweighted.

Dijkstra's algorithm (single-source shortest paths, non-negative weights):

  1. Set dist[s]=0dist[s] = 0 and dist[v]=∞dist[v] = \infty for every other vertex; mark all vertices unvisited.
  2. Pick the unvisited vertex uu with the smallest dist[u]dist[u] and mark it visited.
  3. For each neighbour vv of uu: if dist[u]+w(u,v)<dist[v]dist[u] + w(u,v) < dist[v], set dist[v]=dist[u]+w(u,v)dist[v] = dist[u] + w(u,v) and prev[v]=uprev[v] = u.
  4. Repeat steps 2 and 3 until all vertices are visited (or the destination is visited).

With a simple array it takes O(V2)O(V^2) time; with a min-heap, O((V+E)log⁡V)O((V+E)\log V).

Example: edges A-B = 4, A-C = 2, B-C = 1, B-D = 5, C-D = 8, C-E = 10, D-E = 2. Source A.

StepVisited vertexABCDE
Start0infinfinfinf
1A042infinf
2C0321012
3B032812
4D032810
5E032810

Shortest distances from A: B = 3 (A-C-B), C = 2, D = 8 (A-C-B-D), E = 10 (A-C-B-D-E).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 5 marks

Explain B-tree of order 3 with example.

Answer

A B-tree of order mm is a balanced multiway search tree in which:

  • every node has at most mm children and m−1m-1 keys;
  • every non-root internal node has at least ⌈m/2⌉\lceil m/2 \rceil children;
  • the root has at least 2 children (unless it is a leaf);
  • all leaves are at the same level;
  • keys inside a node are sorted, and a node with kk keys has k+1k+1 children.

For order 3, each node holds 1 or 2 keys and has 2 or 3 children (a 2-3 tree). It is widely used in databases and file systems because the height is small, which reduces disk accesses.

Insertion: insert the key in the proper leaf. If the leaf overflows (3 keys), split it and push the middle key up to the parent; splits may propagate to the root, which is the only way the tree grows taller.

Example: insert 10, 20, 30, 40, 50, 60, 70.

  1. Insert 10, 20: [10 20].
  2. Insert 30: overflow [10 20 30], so 20 moves up. Root [20], leaves [10] and [30].
  3. Insert 40: leaf [30 40].
  4. Insert 50: leaf [30 40 50] overflows, so 40 moves up. Root [20 40], leaves [10], [30], [50].
  5. Insert 60: leaf [50 60].
  6. Insert 70: leaf [50 60 70] overflows, so 60 moves up and the root becomes [20 40 60], which overflows, so 40 moves up to a new root.

Final tree:

              [40]
           /        \
        [20]        [60]
        /  \        /  \
     [10] [30]   [50] [70]

All leaves are at the same level and every node satisfies the order-3 rules.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What is data structure?

Answer

A data structure is a particular way of organising, storing and managing data in a computer so that it can be accessed and modified efficiently. Examples are arrays, linked lists, stacks, queues, trees and graphs. They are classified as linear (array, list, stack, queue) and non-linear (tree, graph).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Why is Big Oh notation used?

Answer

Big Oh notation gives an upper bound on the growth rate of an algorithm's running time or space as the input size nn grows. It lets us compare algorithms independently of hardware, language and constant factors, and tells us the worst-case behaviour, e.g. f(n)=3n2+5n+2f(n)=3n^2+5n+2 is O(n2)O(n^2).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Why are linked lists preferred over arrays?

Answer

Linked lists are preferred when the size is not known in advance or changes often. They grow and shrink dynamically, so no memory is wasted or overflowed, and insertion or deletion needs only pointer changes (O(1)O(1) once the position is known) without shifting elements as in an array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What is linear queue?

Answer

A linear queue is a linear data structure that follows the FIFO (First In First Out) principle. Insertion (enqueue) is done at the rear and deletion (dequeue) at the front. Its drawback is that once rear reaches the last index, no more insertions are possible even if front positions have been freed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Define activation record.

Answer

An activation record (stack frame) is the block of memory created on the call stack each time a function is called. It stores the function's parameters, local variables, return address and saved registers, and is removed when the function returns. In recursion, each call gets its own activation record.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What is B tree?

Answer

A B-tree is a balanced multi-way search tree in which every node can hold several keys and have several children, and all leaves are at the same level. Keys inside a node are kept sorted, and the tree stays balanced through node splitting and merging. It is widely used for disk-based indexes and databases because it keeps the height small, so fewer disk accesses are needed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark
  • 2016 AD · 1 mark

For which purpose Kruskal's algorithm is used?

Answer

Kruskal's algorithm is used to find the minimum spanning tree (MST) of a connected, weighted, undirected graph. It repeatedly picks the smallest-weight edge that does not form a cycle until V−1V-1 edges are chosen.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What is sorting?

Answer

Sorting is the process of arranging the elements of a list or file in a specified order, either ascending or descending (numerical or alphabetical), according to some key. Examples are bubble sort, merge sort, quick sort and heap sort.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 4 marks

Write a java function to delete the last node from a circular linked list.

Answer

Assume a singly circular linked list whose last node points back to the first node, with head pointing to the first node.

class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; }
}

class CircularList {
    Node head;

    void deleteLast() {
        if (head == null) {                       // empty list
            System.out.println("List is empty");
            return;
        }
        if (head.next == head) {                  // only one node
            head = null;
            return;
        }
        Node prev = head;
        while (prev.next.next != head) {          // stop at second last node
            prev = prev.next;
        }
        prev.next = head;                         // second last now points to head
    }
}

The loop stops at the second-last node, which is then linked to head, so the old last node is dropped. Time complexity is O(n)O(n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 4 marks

Write a program in java to display fourth element of the Fibonacci series.

Answer

Taking the Fibonacci series as 0,1,1,2,3,5,…0, 1, 1, 2, 3, 5, \dots the fourth element is 22.

public class Fibonacci {
    public static void main(String[] args) {
        int n = 4;
        int a = 0, b = 1, result = 0;
        if (n == 1) result = a;
        else if (n == 2) result = b;
        else {
            for (int i = 3; i <= n; i++) {
                result = a + b;
                a = b;
                b = result;
            }
        }
        System.out.println("The " + n + "th element of Fibonacci series is " + result);
    }
}

Trace: i=3i=3: result =0+1=1=0+1=1; i=4i=4: result =1+1=2=1+1=2. Output: The 4th element of Fibonacci series is 2.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 4 marks

Insert the following data in a Hash Table, where the table size is 10: 22, 18, 37, 88, 50, 47, 69, 72 (Use any suitable technique to resolve hash collision)

Answer

Use the hash function h(k)=k mod 10h(k)=k \bmod 10 and linear probing, i.e. on a collision try (h(k)+i) mod 10(h(k)+i)\bmod 10 for i=1,2,…i=1,2,\dots

KeyHashActionIndex
222free2
188free8
377free7
888collision, 9 free9
500free0
4777, 8, 9, 0 occupied, 1 free1
6999, 0, 1, 2 occupied, 3 free3
7222, 3 occupied, 4 free4

Final table:

Index0123456789
Key5047226972371888

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 4 marks

Show the steps to sort given data using Merge Sort: 36, 48, 23, 59, 68, 44, 97, 99, 91, 18, 33

Answer

Merge sort repeatedly divides the list into halves until single elements remain, then merges the sorted halves.

Divide

  • [36,48,23,59,68,44,97,99,91,18,33][36, 48, 23, 59, 68, 44, 97, 99, 91, 18, 33]
  • [36,48,23,59,68,44][36, 48, 23, 59, 68, 44] and [97,99,91,18,33][97, 99, 91, 18, 33]
  • [36,48,23][36, 48, 23], [59,68,44][59, 68, 44], [97,99,91][97, 99, 91], [18,33][18, 33]
  • [36,48][36, 48], [23][23], [59,68][59, 68], [44][44], [97,99][97, 99], [91][91], [18][18], [33][33]
  • single elements: 36, 48, 23, 59, 68, 44, 97, 99, 91, 18, 33

Merge

  • [36,48]+[23]→[23,36,48][36,48]+[23] \to [23, 36, 48]
  • [59,68]+[44]→[44,59,68][59,68]+[44] \to [44, 59, 68]
  • [97,99]+[91]→[91,97,99][97,99]+[91] \to [91, 97, 99]
  • [18]+[33]→[18,33][18]+[33] \to [18, 33]
  • [23,36,48]+[44,59,68]→[23,36,44,48,59,68][23,36,48]+[44,59,68] \to [23, 36, 44, 48, 59, 68]
  • [91,97,99]+[18,33]→[18,33,91,97,99][91,97,99]+[18,33] \to [18, 33, 91, 97, 99]
  • [23,36,44,48,59,68]+[18,33,91,97,99]→[23,36,44,48,59,68]+[18,33,91,97,99] \to

[18,23,33,36,44,48,59,68,91,97,99][18, 23, 33, 36, 44, 48, 59, 68, 91, 97, 99]

Time complexity is O(nlog⁡n)O(n\log n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 5 marks

Write steps to insert key into B tree.

Answer

Let the B-tree have order mm, so a node holds at most m−1m-1 keys and at most mm children.

Steps

  1. Start at the root and search down the tree, comparing the key with the keys of each node to choose the correct child, until a leaf is reached. (If the key already exists, stop.)
  2. Insert the key into that leaf at its sorted position.
  3. If the leaf now has at most m−1m-1 keys, stop.
  4. If the leaf overflows (has mm keys), split it: take the median key, keep the keys left of the median in one node and the keys right of it in a new node.
  5. Move the median key up into the parent, with the two new nodes as its adjacent children.
  6. If the parent now overflows, repeat the split on the parent, and continue upwards as needed.
  7. If the root splits, create a new root holding the median key. This is the only way the tree grows in height.

Example (order 3, max 2 keys per node): insert 10, 20, 30.

  • 10 and 20 go into the root: [10,20][10, 20].
  • Inserting 30 gives [10,20,30][10, 20, 30], which overflows. The median 20 moves up into a new root, with children [10][10] and [30][30].

Inserting 40 then goes to the leaf [30,40][30, 40], and inserting 50 makes [30,40,50][30, 40, 50], which splits and pushes 40 up to give root [20,40][20, 40] with children [10][10], [30][30], [50][50].

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 5 marks

How a node of a binary tree can be deleted? Explain with examples.

Answer

Deleting a node from a binary (search) tree has three cases, depending on the number of children of the node to be deleted.

Case 1: node is a leaf. Simply remove it by setting the parent's pointer to null.

Example: deleting 20 from the tree below.

    50            50
   /  \    ->    /  \
  30   70       30   70
 /               
20               

Case 2: node has one child. Replace the node by its only child, i.e. link the parent directly to that child.

Example: delete 30 (which has only the left child 20); 50's left pointer now points to 20.

Case 3: node has two children. Replace the node's value with its inorder successor (the smallest node in the right subtree) or its inorder predecessor (the largest in the left subtree), then delete that successor node, which has at most one child and falls under case 1 or 2.

Example:

      50                60
     /  \              /  \
   30    70    ->     30    70
        /  \                 \
      60    80                80

To delete 50, the inorder successor is 60 (the minimum of the right subtree). Copy 60 to the root and delete the original 60 node.

The time taken is O(h)O(h), where hh is the height of the tree.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is doubly linked list?

Answer

A doubly linked list is a linked list in which each node has three fields: the data, a pointer prev to the previous node and a pointer next to the next node. It can therefore be traversed in both forward and backward directions.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What do you mean by cut vertex?

Answer

A cut vertex (articulation point) is a vertex of a connected graph whose removal, together with its incident edges, disconnects the graph or increases the number of connected components.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

Define spanning tree.

Answer

A spanning tree of a connected graph G=(V,E)G=(V,E) is a subgraph that includes all the vertices of GG, is connected and contains no cycle. It has exactly V−1V-1 edges.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is circular queue?

Answer

A circular queue is a queue in which the last position is connected back to the first position, so the array is treated as a circle. When rear reaches the end, it wraps around using rear=(rear+1) mod nrear=(rear+1)\bmod n, so freed positions at the front are reused. This avoids the wasted space of a linear queue.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

Why worst case complexity is mostly used in algorithm design than best and average case complexity?

Answer

Worst-case complexity gives an upper bound that guarantees the algorithm will never take longer than that for any input of size nn. The best case is rarely met and gives no useful guarantee, and the average case needs assumptions about the input distribution and is hard to compute. The worst case is also often close to the average, and it matters for critical applications.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

Define nested recursion.

Answer

Nested recursion is a form of recursion in which a recursive function passes a recursive call to itself as its own argument. An example is the Ackermann function, where A(m,n)=A(m−1,A(m,n−1))A(m,n)=A(m-1,A(m,n-1)) for m,n>0m,n>0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is hashing? Write down various approaches used for resolving hash collision.

Answer

Hashing is a technique for storing and retrieving data directly using a hash function h(k)h(k) that maps a key kk to an index of a hash table, giving average access time of O(1)O(1). A collision occurs when two different keys map to the same index.

Collision resolution approaches

  1. Separate chaining (open hashing): each table slot holds a linked list of all the keys that hash to that slot.
  2. Open addressing (closed hashing): all keys are stored in the table itself, and another empty slot is probed on a collision:
    • Linear probing: try (h(k)+i) mod m(h(k)+i)\bmod m for i=1,2,…i=1,2,\dots
    • Quadratic probing: try (h(k)+i2) mod m(h(k)+i^2)\bmod m.
    • Double hashing: try (h1(k)+i⋅h2(k)) mod m(h_1(k)+i\cdot h_2(k))\bmod m using a second hash function.
  3. Rehashing: build a larger table with a new hash function when the table becomes too full.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What do you mean by order of B tree?

Answer

The order of a B-tree is the maximum number of children a node can have. A B-tree of order mm has at most mm children and m−1m-1 keys per node, and every non-root internal node has at least ⌈m/2⌉\lceil m/2\rceil children.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is heap?

Answer

A heap is a complete binary tree in which every node satisfies the heap property. In a max-heap each parent is greater than or equal to its children, so the largest element is at the root. In a min-heap each parent is less than or equal to its children, so the smallest is at the root. It is commonly stored in an array and used in heap sort and priority queues.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 4 marks

Write a Java function to delete an element at any position from circular linked list.

Answer

Assume a singly circular linked list with head pointing to the first node and positions numbered from 1.

class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; }
}

class CircularList {
    Node head;

    void deleteAt(int pos) {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        if (pos == 1) {                           // delete first node
            if (head.next == head) {              // only one node
                head = null;
            } else {
                Node last = head;
                while (last.next != head) last = last.next;
                head = head.next;
                last.next = head;
            }
            return;
        }
        Node prev = head;
        for (int i = 1; i < pos - 1; i++) {       // move to node before target
            prev = prev.next;
            if (prev == head) {
                System.out.println("Invalid position");
                return;
            }
        }
        Node target = prev.next;
        if (target == head) {
            System.out.println("Invalid position");
            return;
        }
        prev.next = target.next;                  // unlink target
    }
}

For position 1 the last node must be re-linked to the new head; for any other position the previous node simply skips the target. Time complexity is O(n)O(n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 4 marks

Sort the following given data using Quick sort algorithm. 5, 66, 3, 8, 91, 45, 88, 91, 33

Answer

Quick sort picks the first element as pivot, places elements smaller than the pivot to its left and the rest to its right, then sorts each part recursively.

Data: 5,66,3,8,91,45,88,91,335, 66, 3, 8, 91, 45, 88, 91, 33

  1. Pivot 5: left [3][3], right [66,8,91,45,88,91,33][66, 8, 91, 45, 88, 91, 33], giving [3] 5 [66,8,91,45,88,91,33][3]\ 5\ [66, 8, 91, 45, 88, 91, 33].
  2. Sort the right part, pivot 66: left [8,45,33][8, 45, 33], right [91,88,91][91, 88, 91].
  3. Left part [8,45,33][8, 45, 33], pivot 8: left empty, right [45,33][45, 33].
  4. [45,33][45, 33], pivot 45: left [33][33], giving 33,4533, 45. So the left part becomes 8,33,458, 33, 45.
  5. Right part [91,88,91][91, 88, 91], pivot 91: left [88][88], right [91][91], giving 88,91,9188, 91, 91.

Combining everything:

3,5,8,33,45,66,88,91,913, 5, 8, 33, 45, 66, 88, 91, 91

Average time is O(nlog⁡n)O(n\log n) and worst case is O(n2)O(n^2).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 4 marks

Write all possible topological sorts from given graph. (A directed graph on vertices A-F with edges A->B, A->D, B->C, D->E, E->C, F->D is given in a figure in the original paper.)

Answer

The edges are A→BA\to B, A→DA\to D, B→CB\to C, D→ED\to E, E→CE\to C, F→DF\to D. The constraints are: AA and FF come before DD; DD before EE; EE and BB before CC; AA before BB.

In-degrees: A=0A=0, F=0F=0, B=1B=1, D=2D=2, E=1E=1, C=2C=2. So AA and FF are the possible starting vertices. Repeatedly removing a vertex with in-degree 0 and exploring all choices gives exactly 7 orderings:

  1. A,B,F,D,E,CA, B, F, D, E, C
  2. A,F,B,D,E,CA, F, B, D, E, C
  3. A,F,D,B,E,CA, F, D, B, E, C
  4. A,F,D,E,B,CA, F, D, E, B, C
  5. F,A,B,D,E,CF, A, B, D, E, C
  6. F,A,D,B,E,CF, A, D, B, E, C
  7. F,A,D,E,B,CF, A, D, E, B, C

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 5 marks

What is problem with Binary tree? Explain self-adjusting tree with example.

Answer

Problem with binary (search) trees: their performance depends on the shape of the tree. Search, insertion and deletion take O(h)O(h), and if keys arrive in sorted or nearly sorted order the tree degenerates into a skewed tree, like a linked list, with height nn, so operations become O(n)O(n) instead of O(log⁡n)O(\log n). Plain binary trees also do not exploit the fact that some nodes are accessed more often than others.

Self-adjusting tree: a binary search tree that restructures itself after each access so that frequently accessed nodes move nearer to the root. It needs no balance information stored in the nodes. The common approaches are:

  • Move-to-root: after accessing a node, rotate it up until it becomes the root.
  • Splay tree: after accessing a node, perform splaying, a sequence of rotations (zig, zig-zig, zig-zag) that brings it to the root. This gives O(log⁡n)O(\log n) amortized time per operation.

Example: in the chain 10 (root) with right child 20, whose right child is 30, accessing 30 splays it:

10                      30
  \                    /
   20     splay 30    20
     \    ------->   /
      30            10

Here a zig-zig step rotates the parent and grandparent in turn, so 30 becomes the root. Repeated accesses to 30 are then O(1)O(1), and splaying a deeper node also roughly halves the depth of the nodes along its access path, which keeps the amortized cost at O(log⁡n)O(\log n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 5 marks

What is the limitation of B-tree? How is element of B tree deleted? Explain with example.

Answer

Limitations of B-tree

  • Nodes are not always full, so up to about half of the node space can be wasted.
  • Insertion and deletion are complex (splitting, borrowing and merging).
  • Searching within a node is needed, so in-memory use is less efficient than for simpler trees.
  • Keys and data are stored in internal nodes too, so sequential access to all records is awkward (this is solved by the B+ tree).

Deletion of a key (order mm; every non-root node must keep at least ⌈m/2⌉−1\lceil m/2\rceil-1 keys)

  1. Search for the key.
  2. If it is in a leaf, remove it. If the leaf still has the minimum number of keys, stop.
  3. If it is in an internal node, replace it by its inorder predecessor or successor (taken from a leaf), and delete that key from the leaf.
  4. If a node has fewer than the minimum keys (underflow):
    • Borrow: if an adjacent sibling has more than the minimum, rotate a key from the sibling through the parent.
    • Merge: otherwise merge the node with a sibling and the separating key from the parent. This may cause an underflow in the parent, so repeat upwards.
  5. If the root becomes empty, the merged node becomes the new root and the height decreases by one.

Example (order 3, max 2 keys, min 1 key): root [20,40][20, 40] with leaves [10][10], [30][30], [50][50].

  • Delete 30: the leaf becomes empty (underflow), and its siblings [10][10] and [50][50] have only the minimum, so merge. The leaf [30][30] merges with [10][10] and the separator 20, giving [10,20][10, 20]. The root is now [40][40] with children [10,20][10, 20] and [50][50].
  • Delete 10 next: the leaf [10,20][10,20] becomes [20][20], which is fine.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

Define data structure.

Answer

A data structure is a particular way of organizing, storing and managing data in a computer so that it can be accessed and modified efficiently. Examples are arrays, linked lists, stacks, queues, trees and graphs.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

How does stack differ from queue?

Answer

A stack follows LIFO (Last In First Out): insertion and deletion both happen at one end, the top, using push and pop. A queue follows FIFO (First In First Out): insertion happens at the rear and deletion at the front, using enqueue and dequeue.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

Why is doubly linked list better than singly linked list?

Answer

A doubly linked list can be traversed in both directions because each node has prev and next pointers. A node can also be deleted given only a pointer to it, since its predecessor is directly available, whereas in a singly linked list the list must be traversed to find the previous node. The cost is extra memory for the additional pointer.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

List the advantages of recursion over loop.

Answer

  • Recursive code is shorter, simpler and easier to read and understand.
  • It is the natural way to express problems that are defined recursively, such as tree traversal, factorial, Tower of Hanoi and divide-and-conquer algorithms.
  • It reduces the need to manage loop variables and temporary state explicitly, so there are fewer chances of logical errors.
  • Backtracking problems are much easier to write recursively.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What is Binary Search Tree?

Answer

A Binary Search Tree (BST) is a binary tree in which, for every node, all keys in its left subtree are smaller than the node's key and all keys in its right subtree are larger. An inorder traversal gives the keys in sorted order, and search, insertion and deletion take O(h)O(h) time, where hh is the height.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What is multi-way tree?

Answer

A multi-way tree (m-way tree) is a tree in which each node can have more than two children, up to mm children and m−1m-1 keys, with the keys in a node kept sorted. It generalises the binary search tree, and B-trees are balanced multi-way search trees.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

Define weighted graph.

Answer

A weighted graph is a graph G=(V,E)G=(V,E) in which each edge is assigned a numerical value called its weight (cost, distance or time). Weighted graphs are used in problems such as shortest path and minimum spanning tree.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

In how many ways graph can be represented in computer?

Answer

A graph can be represented in a computer in two main ways: the adjacency matrix, which is a V×VV\times V array where entry (i,j)(i,j) shows whether an edge exists, and the adjacency list, which keeps a linked list of the neighbours of each vertex. (An incidence matrix or an edge list can also be used.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What is tail recursion?

Answer

Tail recursion is a form of recursion in which the recursive call is the very last operation performed by the function, so nothing remains to be done after the call returns. It can be converted easily into a loop and optimised by compilers to avoid growing the call stack. For example, gcd(a,b) returning gcd(b, a % b) is tail recursive.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

List the different types of hash function.

Answer

Common hash functions are:

  • Division method: h(k)=k mod mh(k) = k \bmod m.
  • Multiplication method: h(k)=⌊m (kA mod 1)⌋h(k) = \lfloor m\,(kA \bmod 1)\rfloor, with 0<A<10<A<1.
  • Mid-square method: square the key and take the middle digits as the address.
  • Folding method: split the key into parts, add them (shift or boundary folding) and take the result modulo table size.
  • Digit analysis and truncation methods: select or drop certain digits of the key.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 4 marks

Write a Java function to demonstrate push, pop and traversal operations of a stack.

Answer

A stack can be implemented with an array and a top index. Push adds at top, pop removes from top, and traversal prints elements from top to bottom.

class Stack {
    int[] data = new int[100];
    int top = -1;

    void push(int x) {
        if (top == data.length - 1) {
            System.out.println("Stack overflow");
            return;
        }
        data[++top] = x;
    }

    int pop() {
        if (top == -1) {
            System.out.println("Stack underflow");
            return -1;
        }
        return data[top--];
    }

    void traverse() {
        if (top == -1) {
            System.out.println("Stack is empty");
            return;
        }
        for (int i = top; i >= 0; i--)
            System.out.print(data[i] + " ");
        System.out.println();
    }
}

Each of push and pop takes O(1)O(1) time, and traversal takes O(n)O(n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 4 marks

Write a java function to insert an element at specific position in a doubly linked list.

Answer

class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; }
}

class DoublyLinkedList {
    Node head;

    // insert value x at position pos (1-based)
    void insertAt(int x, int pos) {
        Node n = new Node(x);
        if (pos == 1) {                  // insert at beginning
            n.next = head;
            if (head != null) head.prev = n;
            head = n;
            return;
        }
        Node cur = head;
        for (int i = 1; i < pos - 1 && cur != null; i++)
            cur = cur.next;
        if (cur == null) {
            System.out.println("Invalid position");
            return;
        }
        n.next = cur.next;
        n.prev = cur;
        if (cur.next != null) cur.next.prev = n;
        cur.next = n;
    }
}

The function walks to the node just before the required position, then links the new node by setting its next and prev pointers and updating the neighbouring nodes. Time complexity is O(n)O(n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 4 marks

Create a max heap from the following: Show all steps. 17, 27, 16, 29, 35, 20, 75, 18, 35

Answer

Insert the keys one by one at the next free leaf and sift up while the node is larger than its parent. The array is shown after each insertion.

  1. Insert 17: [17]
  2. Insert 27: it is greater than 17, so swap: [27, 17]
  3. Insert 16: [27, 17, 16]
  4. Insert 29: swap with parent 17, then with parent 27: [29, 27, 16, 17]
  5. Insert 35: swap with parent 27, then with 29: [35, 29, 16, 17, 27]
  6. Insert 20: swap with parent 16: [35, 29, 20, 17, 27, 16]
  7. Insert 75: swap with parent 20, then with 35: [75, 29, 35, 17, 27, 16, 20]
  8. Insert 18: swap with parent 17: [75, 29, 35, 18, 27, 16, 20, 17]
  9. Insert 35: swap with parent 18, then with 29 (the parent 75 is larger, so stop): [75, 35, 35, 29, 27, 16, 20, 17, 18]

Final max heap (array form): 75, 35, 35, 29, 27, 16, 20, 17, 18.

            75
          /    \
        35      35
       /  \    /  \
     29   27  16   20
    /  \
  17   18

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 4 marks

Write a hash function to insert element to list.

Answer

Using the division method with linear probing to resolve collisions:

class HashTable {
    int size = 10;
    int[] table = new int[size];

    HashTable() {
        java.util.Arrays.fill(table, -1);   // -1 means empty
    }

    int hash(int key) {
        return key % size;
    }

    void insert(int key) {
        int index = hash(key);
        int count = 0;
        while (table[index] != -1 && count < size) {
            index = (index + 1) % size;      // linear probing
            count++;
        }
        if (count == size) {
            System.out.println("Hash table is full");
            return;
        }
        table[index] = key;
    }
}

hash() maps the key to a slot. If the slot is taken, insert() moves to the next slot (wrapping around) until a free one is found.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 4 marks

Sort the following data using Radix Sort: Show all steps. 23, 76, 53, 40, 67, 1, 213, 21

Answer

Radix sort processes digits from the least significant to the most significant, using a stable bucket pass for each digit.

Data: 23, 76, 53, 40, 67, 1, 213, 21

Pass 1 (units digit):

  • 0: 40
  • 1: 1, 21
  • 3: 23, 53, 213
  • 6: 76
  • 7: 67

Result: 40, 1, 21, 23, 53, 213, 76, 67

Pass 2 (tens digit):

  • 0: 1
  • 1: 213
  • 2: 21, 23
  • 4: 40
  • 5: 53
  • 6: 67
  • 7: 76

Result: 1, 213, 21, 23, 40, 53, 67, 76

Pass 3 (hundreds digit):

  • 0: 1, 21, 23, 40, 53, 67, 76
  • 2: 213

Result: 1, 21, 23, 40, 53, 67, 76, 213

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 5 marks

What is B tree? What are the benefits of B tree over Binary Search tree?

Answer

B-tree: a B-tree of order mm is a self-balancing multiway search tree in which:

  • every node has at most mm children and at most m−1m-1 keys, stored in sorted order;
  • every non-root internal node has at least ⌈m/2⌉\lceil m/2 \rceil children, and the root has at least 2 children if it is not a leaf;
  • all leaves are on the same level;
  • the keys of a node separate the key ranges of its children, as in a search tree.

Insertion splits a full node and pushes the middle key up. Deletion borrows from or merges with siblings. Both keep the tree balanced.

Benefits over a binary search tree:

  1. A BST can become skewed (height O(n)O(n)), while a B-tree is always balanced with height O(log⁡n)O(\log n).
  2. A B-tree node holds many keys, so the height is much smaller and fewer node accesses are needed. This matters when nodes are disk blocks, since each access is costly.
  3. It suits databases and file systems that store large data on secondary storage.
  4. Search, insertion and deletion are all guaranteed O(log⁡n)O(\log n) in the worst case.
  5. Nodes are kept at least half full, which makes good use of space.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 5 marks

How cycle in a graph can be detected? Explain depth first search.

Answer

Depth first search (DFS) visits a graph by going as deep as possible along each branch before backtracking. Starting from a vertex, it marks the vertex visited, then recursively visits each unvisited neighbour (or uses an explicit stack). When a vertex has no unvisited neighbours, the search backtracks to the previous vertex.

DFS(v):
    mark v as visited
    for each neighbour w of v:
        if w is not visited:
            DFS(w)

Its time complexity is O(V+E)O(V+E) with an adjacency list.

Cycle detection using DFS:

  • Undirected graph: run DFS, tracking the parent of each vertex. If a vertex has a visited neighbour that is not its parent, a back edge exists, so the graph has a cycle.
  • Directed graph: keep a "currently in recursion stack" flag for each vertex. If DFS reaches a vertex that is already in the recursion stack, a back edge exists and there is a cycle. Equivalently, if a topological sort cannot include all vertices, the graph has a cycle.

If DFS finishes with no back edge, the graph is acyclic.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

What are rear and front in queue?

Answer

In a queue, front is the end from which elements are removed (dequeued), and rear is the end at which new elements are inserted (enqueued). A queue follows the FIFO (first in, first out) rule.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

Define big Oh notation

Answer

Big Oh notation gives an asymptotic upper bound on an algorithm's running time. f(n)=O(g(n))f(n) = O(g(n)) if there exist positive constants cc and n0n_0 such that f(n)≤c g(n)f(n) \le c\,g(n) for all n≥n0n \ge n_0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

Define recursion.

Answer

Recursion is a technique in which a function calls itself, directly or indirectly, to solve a smaller instance of the same problem. It needs a base case to stop the calls and a recursive case that reduces the problem, for example n!=n×(n−1)!n! = n\times(n-1)! with 0!=10! = 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

What is B-tree?

Answer

A B-tree is a self-balancing multiway search tree of order mm in which each node holds up to m−1m-1 sorted keys and up to mm children, and all leaves are at the same level. It gives O(log⁡n)O(\log n) search, insertion and deletion and is widely used in databases and file systems.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

Define breadth first traversal

Answer

Breadth first traversal visits a graph level by level. It starts from a source vertex, visits all its neighbours, then all their unvisited neighbours, and so on, using a queue. Its time complexity is O(V+E)O(V+E).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

What is Linked list?

Answer

A linked list is a linear data structure made of nodes, where each node stores data and a reference (pointer) to the next node. The nodes need not be contiguous in memory, so insertion and deletion are easy and the size can grow dynamically. Types include singly, doubly and circular linked lists.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

Write the advantages of binary search over the linear search.

Answer

  • Binary search takes O(log⁡n)O(\log n) comparisons, while linear search takes O(n)O(n), so it is much faster for large data.
  • It halves the search range at each step, so it needs very few comparisons (about 20 for a million items).

It requires the data to be sorted and to allow direct access, as in an array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

What is shortest path problem?

Answer

The shortest path problem is the problem of finding a path between two vertices of a weighted graph so that the sum of the edge weights along the path is minimum. Dijkstra's algorithm (non-negative weights) and the Bellman-Ford algorithm are used to solve it.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 1 mark

What is AVL tree?

Answer

An AVL tree is a self-balancing binary search tree in which the heights of the left and right subtrees of every node differ by at most 1. The balance factor (height of left subtree minus height of right subtree) of every node is −1-1, 00 or +1+1. When an insertion or deletion breaks this, rotations restore balance, so operations remain O(log⁡n)O(\log n).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 4 marks

Write a java class to implement stack with push and pop functions.

Answer

class Stack {
    private int[] arr;
    private int top;
    private int capacity;

    Stack(int size) {
        capacity = size;
        arr = new int[size];
        top = -1;
    }

    boolean isEmpty() { return top == -1; }
    boolean isFull()  { return top == capacity - 1; }

    void push(int x) {
        if (isFull()) {
            System.out.println("Stack overflow");
            return;
        }
        arr[++top] = x;
    }

    int pop() {
        if (isEmpty()) {
            System.out.println("Stack underflow");
            return -1;
        }
        return arr[top--];
    }

    public static void main(String[] args) {
        Stack s = new Stack(5);
        s.push(10);
        s.push(20);
        s.push(30);
        System.out.println(s.pop());   // 30
        System.out.println(s.pop());   // 20
    }
}

Both push and pop run in O(1)O(1) time. The stack is LIFO: the last element pushed is the first popped.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 4 marks

Write a method to insert a node in circular doubly linked list at end. Also make appropriate assumptions.

Answer

Assumptions: each node has data, prev and next; head points to the first node; the list is circular, so head.prev is the last node and the last node's next is head.

class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; }
}

class CircularDLL {
    Node head;

    void insertAtEnd(int x) {
        Node n = new Node(x);
        if (head == null) {          // empty list
            n.next = n;
            n.prev = n;
            head = n;
            return;
        }
        Node last = head.prev;       // current last node
        n.next = head;
        n.prev = last;
        last.next = n;
        head.prev = n;
    }
}

Because the last node is reached through head.prev, the insertion takes O(1)O(1) time.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 4 marks

Write a hash method to insert following data in a Hash Table, of size 10: 24, 20, 37, 84, 50, 47, 67, and 74

Answer

Hash function: h(k)=k mod 10h(k) = k \bmod 10, with linear probing for collisions.

int[] table = new int[10];          // 0 means empty (no key is 0)

void insert(int key) {
    int i = key % 10;
    while (table[i] != 0)
        i = (i + 1) % 10;           // linear probing
    table[i] = key;
}

Inserting 24, 20, 37, 84, 50, 47, 67, 74:

Keyk mod 10k \bmod 10Placed at
2444
2000
3777
844 (occupied)5
500 (occupied)1
477 (occupied)8
677, 8 (occupied)9
744, 5 (occupied)6

Final table:

Index0123456789
Key2050--248474374767

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 4 marks

Write java function or algorithm for finding the minimum spanning tree using Kruskal's algorithm.

Answer

Kruskal's algorithm builds a minimum spanning tree by sorting all edges by weight and adding each edge in turn if it does not form a cycle. Cycles are detected with a disjoint-set (union-find) structure.

Algorithm:

  1. Sort all edges in non-decreasing order of weight.
  2. Make each vertex its own set.
  3. For each edge (u,v)(u,v) in order: if find(u) differs from find(v), add the edge to the tree and union the sets.
  4. Stop when V−1V-1 edges have been chosen.
import java.util.*;

class Edge {
    int u, v, w;
    Edge(int u, int v, int w) { this.u = u; this.v = v; this.w = w; }
}

class Kruskal {
    static int[] parent;

    static int find(int x) {
        if (parent[x] != x) parent[x] = find(parent[x]);   // path compression
        return parent[x];
    }

    static void kruskal(int V, List<Edge> edges) {
        Collections.sort(edges, (a, b) -> a.w - b.w);
        parent = new int[V];
        for (int i = 0; i < V; i++) parent[i] = i;

        int count = 0, total = 0;
        for (Edge e : edges) {
            int ru = find(e.u), rv = find(e.v);
            if (ru != rv) {
                parent[ru] = rv;
                System.out.println(e.u + " - " + e.v + " : " + e.w);
                total += e.w;
                if (++count == V - 1) break;
            }
        }
        System.out.println("Total weight = " + total);
    }
}

Time complexity is O(Elog⁡E)O(E \log E), dominated by sorting.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 4 marks

Explain topological sort with example.

Answer

Topological sort is a linear ordering of the vertices of a directed acyclic graph (DAG) such that for every edge u→vu \to v, vertex uu comes before vv. It exists only if the graph has no cycle. It is used for task scheduling, course prerequisites and build order.

Algorithm (Kahn's, using in-degrees):

  1. Compute the in-degree of every vertex.
  2. Put all vertices of in-degree 0 into a queue.
  3. Remove a vertex from the queue, output it, and reduce the in-degree of its neighbours by 1. Enqueue any that become 0.
  4. Repeat until the queue is empty. If fewer than all vertices were output, the graph has a cycle.

Alternatively, run DFS and output vertices in reverse order of finishing time.

Example: edges A→BA\to B, A→CA\to C, B→DB\to D, C→DC\to D.

  • In-degrees: A=0, B=1, C=1, D=2.
  • Output A; B and C drop to 0.
  • Output B (D becomes 1), then C (D becomes 0).
  • Output D.

Topological order: A, B, C, D (A, C, B, D is also valid).

Time complexity is O(V+E)O(V+E).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 5 marks

Is O(n2)O(n^2) algorithm better than O(nlog⁡n)O(n\log n) algorithm? Explain with example.

Answer

No. For large inputs an O(nlog⁡n)O(n\log n) algorithm is better than an O(n2)O(n^2) algorithm, because nlog⁡nn\log n grows much more slowly than n2n^2. Big Oh describes the growth rate for large nn and hides constant factors, so for small nn an O(n2)O(n^2) algorithm with a small constant can still be faster.

Example (comparison counts, ignoring constants):

nnn2n^2nlog⁡2nn\log_2 n
10100about 33
1,0001,000,000about 9,966
1,000,000101210^{12}about 2×1072\times10^{7}

Bubble sort (O(n2)O(n^2)) on one million items needs about 101210^{12} steps. Merge sort (O(nlog⁡n)O(n\log n)) needs about 2×1072\times10^{7} steps, which is roughly 50,000 times fewer.

The reverse can hold for small nn. For example, if the O(n2)O(n^2) algorithm takes n2n^2 steps and the O(nlog⁡n)O(n\log n) one takes 100 nlog⁡2n100\,n\log_2 n steps, the quadratic one is faster for small nn (below about 600) because of the large constant. Beyond the crossover point, O(nlog⁡n)O(n\log n) wins and the gap keeps widening.

So O(nlog⁡n)O(n\log n) is better asymptotically, but it need not win for small inputs.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2019 AD · 5 marks

Write limitation of binary search tree? Explain why concept of splaying is used in trees?

Answer

Limitations of a binary search tree (BST):

  1. The shape depends on the order of insertion. Inserting sorted data (1, 2, 3, ...) produces a skewed tree like a linked list, with height nn.
  2. In that worst case, search, insertion and deletion take O(n)O(n) instead of O(log⁡n)O(\log n).
  3. There is no automatic balancing, so performance is unpredictable.
  4. Each node holds only one key, so for large data on disk the tree is deep and needs many accesses.

Splaying: a splay tree is a self-adjusting BST. After every access (search, insert or delete), the accessed node is moved to the root by a sequence of rotations (zig, zig-zig, zig-zag). Splaying is used because:

  • recently accessed items stay near the root, so repeated or nearby accesses are fast (locality of reference);
  • it keeps the tree roughly balanced without storing height or balance information, so no extra memory is needed;
  • the amortized cost of each operation is O(log⁡n)O(\log n), even though a single operation may take O(n)O(n);
  • the rotations also shorten long paths, so a skewed tree gradually becomes more balanced.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

Questions from TU BIM Fourth Semester ACC 202 Cost and Management Accounting Model Question 2024 (Faculty of Management model question 2024, BIM fourth semester (new course), via EducateNepal), TU BIM Fourth Semester ECO 206 Economics for Business Model Question 2024 (Faculty of Management model question 2024, BIM fourth semester (new course), via EducateNepal), TU BIM Fourth Semester IT 239 Web Technology II Model Question 2024 (Faculty of Management model question 2024, BIM fourth semester (new course), via EducateNepal), TU BIM Fourth Semester IT 240 Business Data Communication and Networking Model Question 2024 (Faculty of Management model question 2024, BIM fourth semester (new course), via EducateNepal), TU BIM Fourth Semester IT 220 Database Management System Model Question 2024 (Faculty of Management model question 2024, BIM fourth semester (new course), via EducateNepal), TU BIM Fourth Semester IT 241 Operating System Model Question 2024 (Faculty of Management model question 2024, BIM fourth semester (new course), via EducateNepal), TU BBA/BIM 4th Semester Cost and Management Accounting 2014 (model question) (Faculty of Management model question 2014 (old course), via EducateNepal), TU BIM Data Structure and Algorithm with Java (IT 218) 2015 AD (Board exam paper, 2015 AD), TU BIM Data Structure and Algorithm with Java (IT 218) 2016 AD (Board exam paper, 2016 AD), TU BIM Data Structure and Algorithm with Java (IT 218) 2017 AD (Board exam paper, 2017 AD), TU BIM Data Structure and Algorithm with Java (IT 218) 2018 AD (Board exam paper, 2018 AD), TU BIM Data Structure and Algorithm with Java (IT 218) 2019 AD (Board exam paper, 2019 AD), TU BIM Web Programming II (IT 219) 2015 AD (Board exam paper, 2015 AD), TU BIM Web Programming II (IT 219) 2016 AD (Board exam paper, 2016 AD), TU BIM Web Programming II (IT 219) 2017 AD (Board exam paper, 2017 AD), TU BIM Web Programming II (IT 219) 2018 AD (Board exam paper, 2018 AD), TU BIM Web Programming II (IT 219) 2019 AD (Board exam paper, 2019 AD), TU BIM Database Management System (ITC 218) 2007 AD (Board exam paper, 2007 AD), TU BIM Database Management System (ITC 218) 2008 AD (Board exam paper, 2008 AD), TU BIM Database Management System (ITC 218) 2009 AD (Board exam paper, 2009 AD), TU BIM Database Management System (ITC 218) 2010 AD (Board exam paper, 2010 AD), TU BIM Database Management System (ITC 218) 2011 AD (Board exam paper, 2011 AD), TU BIM Database Management System (ITC 218) 2012 AD (Board exam paper, 2012 AD), TU BIM Database Management System (ITC 218) 2013 AD (Board exam paper, 2013 AD), TU BIM Database Management System (ITC 218) 2014 AD (Board exam paper, 2014 AD), TU BIM Database Management System (ITC 218) 2015 AD (Board exam paper, 2015 AD), TU BIM Database Management System (ITC 218) 2016 AD (Board exam paper, 2016 AD), TU BIM Database Management System (ITC 218) 2017 AD (Board exam paper, 2017 AD), TU BIM Database Management System (ITC 218) 2018 AD (Board exam paper, 2018 AD), TU BIM Database Management System (ITC 218) 2019 AD (Board exam paper, 2019 AD), TU BIM Micro Economics (ECO 201) 2007 AD (Board exam paper, 2007 AD), TU BIM Micro Economics (ECO 201) 2008 AD (Board exam paper, 2008 AD), TU BIM Micro Economics (ECO 201) 2009 AD (Board exam paper, 2009 AD), TU BIM Micro Economics (ECO 201) 2010 AD (Board exam paper, 2010 AD), TU BIM Micro Economics (ECO 201) 2014 AD (Board exam paper, 2014 AD), TU BIM Micro Economics (ECO 201) 2015 AD (Board exam paper, 2015 AD), TU BIM Micro Economics (ECO 201) 2016 AD (Board exam paper, 2016 AD), TU BIM Micro Economics (ECO 201) 2017 AD (Board exam paper, 2017 AD), TU BIM Micro Economics (ECO 201) 2018 AD (Board exam paper, 2018 AD) and TU BIM Micro Economics (ECO 201) 2019 AD (Board exam paper, 2019 AD). Answers are written for this site; check them against your class notes.