MCQ Questions

Question Bank › Technical › Data Structures › MCQ

1234

Queue using Linked List

Q101.Easy
In a linked-list implementation of a queue, from which end is an element removed (dequeued)?
  1. At the head (front) of the linked list
  2. At the middle node of the linked list
  3. At the tail (rear) of the linked list
  4. At the node just before the tail
Show answer & explanation
Answer: (A) At the head (front) of the linked list
Explanation: A queue works on FIFO order, so the element that entered first must leave first. In a linked-list queue, the front of the queue is kept at the head of the list. Therefore dequeue removes the node at the head of the linked list. Insertion (enqueue) happens at the tail/rear.
Q102.Medium
In a linked-list implementation of a queue where only the front (head) pointer is maintained (no rear pointer), which operation takes worst-case linear time?
  1. Insertion (enqueue)
  2. Deletion (dequeue)
  3. Checking whether the queue is empty
  4. Deletion and checking whether the queue is empty
Show answer & explanation
Answer: (A) Insertion (enqueue)
Explanation: 1) Deletion happens at the front, and the front pointer is available, so dequeue is O(1). 2) Checking emptiness only compares front to NULL, also O(1). 3) To insert at the rear, we have no rear pointer, so we must walk from front to the last node. 4) That traversal is O(n), i.e. linear time. 5) Hence insertion is the worst-case linear-time operation.

Sparse Arrays

Q103.Easy
In data structures, what is a sparse array?
  1. A structure used to store a collection of full records
  2. A structure that stores individual bits compactly
  3. An array in which most of the elements hold the same default value
  4. An array whose memory is allocated only at run time
Show answer & explanation
Answer: (C) An array in which most of the elements hold the same default value
Explanation: Step 1: In a sparse array, the majority of positions are empty or unused. Step 2: These unused positions are set to a default value such as 0 or null. Step 3: Only a few positions carry meaningful data. Step 4: Therefore it is an array where most elements share the same value.
Q104.Easy
In which situation would you prefer to use a sparse array?
  1. When all elements of the array are distinct
  2. When most of the elements hold the default value, usually zero
  3. When the elements are of different data types
  4. When the elements are stored in sorted order
Show answer & explanation
Answer: (B) When most of the elements hold the default value, usually zero
Explanation: 1. A sparse array is one in which the great majority of positions hold a default value (typically zero or null). 2. Storing such an array densely wastes memory on repeated default values. 3. So a sparse representation is used when most entries are the default value. 4. Distinctness, mixed data types or sortedness do not by themselves make an array sparse.
Q105.Easy
What is the key difference between a normal (naive) array and a sparse array?
  1. A sparse array can hold more elements than a normal array
  2. A sparse array is memory efficient
  3. A sparse array is always dynamic
  4. A naive array is more efficient
Show answer & explanation
Answer: (B) A sparse array is memory efficient
Explanation: 1) A naive array reserves space for the entire size regardless of how many values are actually used. 2) A sparse array (e.g., linked-list implementation) stores only non-default values. 3) So it uses less memory when most entries are default/zero. 4) Hence the difference is memory efficiency.
Q106.Easy
An array P is declared as P = {7, null, null, null, null, 4}. If it is treated as a normal array and then as a sparse array, what are its sizes respectively?
  1. 6 and 6
  2. 6 and 2
  3. 2 and 6
  4. 2 and 2
Show answer & explanation
Answer: (B) 6 and 2
Explanation: Step 1: A normal array stores every position, including the null entries, so its size is the total number of slots = 6. Step 2: A sparse array stores only the non-null (non-zero) elements, here 7 and 4. Step 3: So the sparse array size = 2. Step 4: Therefore the sizes are 6 and 2.

Stack Applications

Q107.Easy
Which of the following is NOT an application of a stack?
  1. evaluation of postfix expression
  2. conversion of infix to postfix expression
  3. balancing of symbols
  4. queue of customers at a ticket counter
Show answer & explanation
Answer: (D) queue of customers at a ticket counter
Explanation: Step 1: A stack is LIFO. Postfix evaluation, infix→postfix conversion and symbol balancing all use LIFO behaviour. Step 2: A queue of customers at a ticket counter is FIFO — first come, first served — which is a queue application, not a stack application. Step 3: Hence the ticket-counter queue is NOT a stack application.
Q108.Easy
Which of the following is NOT a natural application of a stack?
  1. Reversing a string
  2. Evaluating a postfix expression
  3. Implementing recursion
  4. Scheduling jobs on a printer
Show answer & explanation
Answer: (D) Scheduling jobs on a printer
Explanation: 1. Reversing a string: push all characters, then pop them — stack application. 2. Postfix evaluation: operands are pushed and popped in LIFO order — stack application. 3. Recursion: the system keeps activation records on a stack — stack application. 4. Job scheduling (e.g., printer queue) serves jobs in arrival order, i.e., FIFO — it uses a queue, not a stack.
Q109.Easy
Which of the following is NOT an application of a stack?
  1. Checking whether brackets in an expression are balanced
  2. Storing local variables during function calls at runtime
  3. Syntax analysis in a compiler
  4. Passing data between two asynchronous processes
Show answer & explanation
Answer: (D) Passing data between two asynchronous processes
Explanation: 1. A stack works on LIFO order and is used for bracket matching, recursion/function-call management (local variables) and compiler syntax analysis. 2. Passing data between two asynchronous processes needs buffering in FIFO order, which is done using a queue, not a stack. 3. Hence the odd one out is data transfer between two asynchronous processes.
Q110.Easy
Which data structure is most suitable for checking whether parentheses in an expression are balanced?
  1. n-ary tree
  2. queue
  3. priority queue
  4. stack
Show answer & explanation
Answer: (D) stack
Explanation: Step 1: Scan the expression left to right. Step 2: For every opening bracket, push it onto a stack. Step 3: For every closing bracket, pop the top of the stack and check it matches. Step 4: If the stack is empty at the end with no mismatch, the parentheses are balanced. Step 5: So a stack is the right structure.
Q111.Medium
Which program correctly prints the index of every matching parenthesis in an expression string? (Indices are 1-based; a '(' is pushed when seen, and on ')' the matching '(' index is popped and printed.)
  1. Push index i when ch=='(' ; on ch==')' pop p and print "')' at index "+(i+1)+" matched with '(' at index "+(p+1); leftover stack entries printed as unmatched '('.
  2. Push index i when ch==')' ; on ch=='(' pop p and print matched.
  3. Push index i when ch=='(' ; on ch==')' pop p and print "')' at index "+i+" matched with '(' at index "+p (no +1).
  4. Push index i when ch=='(' ; on ch==')' pop p and print "')' at index "+(i+1)+" matched with '(' at index "+p (no +1 on p).
Show answer & explanation
Answer: (A) Push index i when ch=='(' ; on ch==')' pop p and print "')' at index "+(i+1)+" matched with '(' at index "+(p+1); leftover stack entries printed as unmatched '('.
Explanation: Step 1: When we see '(' we must remember its position, so push i. Step 2: When we see ')' we pop the most recent unmatched '(' index p — that is its match. Step 3: To display 1-based indices we add 1 to both i and p. Step 4: Any '(' left on the stack at the end has no match, so print them as unmatched. Only the first option follows all these steps correctly.
Q112.MediumAnswer under review
Consider the infix expression: 7 + 2*(5*2-8). Using the standard stack algorithm to convert infix to postfix, what is the maximum number of symbols that appear on the stack at any one time during the conversion?
  1. 1
  2. 2
  3. 3
  4. 4
Show answer & explanation
Answer: (C) 3
Explanation: 1. Scan '7': output 7. 2. Scan '+': push '+'. Stack: + (1 symbol). 3. Scan '2': output 2. 4. Scan '*': push '*'. Stack: +, * (2 symbols). 5. Scan '(': push '('. Stack: +, *, ( (3 symbols). 6. Scan '5': output 5. 7. Scan '*': push '*'. Stack: +, *, (, * (4 symbols). 8. Scan '2': output 2. 9. Scan '-': pop '*' (output), then push '-'. Stack: +, *, (, - (4 symbols). 10. Scan '8': output 8. 11. Scan ')': pop '-' (output), pop '(' (discard). Stack: +, * (2 symbols). 12. End: pop '*' then '+' (output). Maximum stack size = 4.
Q113.Medium
Which data structure is most likely used internally when a recursive algorithm is converted into a non-recursive (iterative) implementation?
  1. Linked List
  2. Stack
  3. Queue
  4. Tree
Show answer & explanation
Answer: (B) Stack
Explanation: 1. Recursion goes forward calling itself, then returns in reverse order. 2. To simulate this without recursion, we must save partial results and restore them in reverse order. 3. A stack does exactly this (LIFO): push state before the 'recursive' step, pop it on return. 4. So a stack is the natural choice.
Q114.Medium
Accessing data stored in a serial access memory (where records are read one after another in the order they were written) is most similar to manipulating data on which structure?
  1. Heap
  2. Binary Tree
  3. Array
  4. Stack
Show answer & explanation
Answer: (D) Stack
Explanation: 1. Serial access memory stores records one after another and they are reached sequentially. 2. A stack also allows access only in sequence (push/pop at one end). 3. Heaps, binary trees and arrays allow random or structured access, not strictly sequential access. 4. Hence the closest match is the stack.
Q115.Medium
Consider the following Java method, where 'arr' is a non-empty integer array and the Stack class creates an array of the specified size with a top pointer, and push/pop behave normally.

public void process(int[] arr)
{
    Stack st = new Stack(arr.length);
    int[] res = new int[arr.length];
    for(int i = 0; i < arr.length; i++)
    {
        st.push(arr[i]);
    }
    for(int i = 0; i < arr.length; i++)
    {
        res[i] = (int)(st.pop());
    }
    System.out.println("output :");
    for(int i = 0; i < res.length; i++)
    {
        System.out.println(res[i]);
    }
}

What does this method do?
  1. Prints alternate elements of the array
  2. Duplicates the given array
  3. Performs parentheses matching
  4. Reverses the array
Show answer & explanation
Answer: (D) Reverses the array
Explanation: 1. All elements of arr are pushed onto the stack in order arr[0], arr[1], ... 2. A stack is LIFO, so the last pushed element (arr[n-1]) comes out first. 3. Popping fills res[0] with arr[n-1], res[1] with arr[n-2], and so on. 4. Thus res holds the array in reverse order, and the method prints the reversed array.

Stack Basics

Q116.Easy
A stack is described as an "ordered" list of entries. What does "ordered" mean here?
  1. A group of stacks can be sorted
  2. Stack entries can be compared using the '<' operator
  3. The entries are kept in a linked list
  4. Entries are inserted one after another in a definite sequence
Show answer & explanation
Answer: (D) Entries are inserted one after another in a definite sequence
Explanation: 1. In a stack, elements are inserted one by one using push and removed one by one using pop. 2. The order is fixed by the LIFO rule: the last pushed element is the first popped. 3. So "ordered" means the entries follow a definite sequential order of insertion/removal, not that they are sorted or comparable.
Q117.Easy
Consider the following C function (headers included, called from main):
#define SIZE 8
typedef struct {
  int top;
  int data[SIZE];
} stk;
int check(stk *s) {
  if (s->top == -1)
    return 1;
  else
    return 0;
}
What does this function check for?
  1. whether the stack is full
  2. whether the index is invalid
  3. whether the stack is empty
  4. whether the stack is infinite
Show answer & explanation
Answer: (C) whether the stack is empty
Explanation: 1) In an array-based stack, 'top' points to the last inserted element. 2) When no element has been inserted, top is initialised to -1. 3) The function returns 1 exactly when top == -1. 4) Therefore it tests whether the stack is empty.
Q118.Easy
In stack terminology, what does 'stack underflow' mean?
  1. trying to read an element from a stack that was never created
  2. pushing an element onto a stack that is already full
  3. popping an element from a stack that is empty
  4. accessing an array index beyond its declared size
Show answer & explanation
Answer: (C) popping an element from a stack that is empty
Explanation: 1) A stack has two main operations: push (add on top) and pop (remove from top). 2) Overflow happens when you push onto a full stack. 3) Underflow happens when you try to pop from a stack that has no elements (top = -1). 4) Hence 'stack underflow' = removing from an empty stack.

Stack Operations

Q119.Easy
A stack contains (from bottom to top): 8, 6, 4, 2. Now the operator '-' is encountered while evaluating a postfix expression. What should be done?
  1. Pop 2 and 4, compute 4-2=2, push 2
  2. Push '-' onto the stack
  3. Pop 8 and 6, compute 8-6=2, push 2
  4. Ignore the operator '-'
Show answer & explanation
Answer: (A) Pop 2 and 4, compute 4-2=2, push 2
Explanation: Step 1: On encountering an operator, pop the top two operands: first pop gives 2 (right operand), second pop gives 4 (left operand). Step 2: Compute 4-2 = 2. Step 3: Push the result 2 back onto the stack.
Q120.Easy
The process of inserting an element into a stack is called:
  1. Create
  2. Push
  3. Evaluation
  4. Pop
Show answer & explanation
Answer: (B) Push
Explanation: Inserting an element into a stack is called push. It adds an element to the top of the stack. If the stack is full, a push operation causes stack overflow.
Q121.Easy
In a stack, the operation that removes the topmost element is called:
  1. Insert
  2. Push
  3. Peek
  4. Pop
Show answer & explanation
Answer: (D) Pop
Explanation: 1) A stack follows LIFO. 2) Push adds an element on top. 3) Peek only reads the top element. 4) Pop removes the topmost (last inserted) element.
Q122.Easy
In a stack, if a user tries to remove (pop) an element from an empty stack, the condition is called:
  1. Underflow
  2. Empty collection
  3. Overflow
  4. Garbage collection
Show answer & explanation
Answer: (A) Underflow
Explanation: 1. Pop removes the top element of a stack. 2. If the stack has no elements, there is nothing to remove. 3. This error condition is called stack underflow. 4. Overflow is the opposite: pushing onto a full stack. Garbage collection is unrelated memory management.
Q123.Easy
A stack in a ticket-booking program is declared with a maximum capacity of 6 elements and currently holds 6 bookings. If the program now attempts to push one more booking onto this stack, what happens?
  1. Stack overflow occurs
  2. The program crashes immediately
  3. Stack underflow occurs
  4. The oldest booking is automatically deleted
Show answer & explanation
Answer: (A) Stack overflow occurs
Explanation: 1. The stack capacity is 6 and it already contains 6 elements, so it is full. 2. A push operation on a full stack cannot store the new element. 3. This condition is called stack overflow. 4. Underflow is the opposite case (pop from empty stack), so it does not apply here.
Q124.Easy
Consider the following operations performed on a stack of size 5:
Push(10);
Pop();
Push(20);
Push(30);
Pop();
Push(40);
Pop();
Pop();
Push(50);
After all operations are completed, how many elements are present in the stack?
  1. 1
  2. 2
  3. 3
  4. 4
Show answer & explanation
Answer: (A) 1
Explanation: 1. Count pushes: Push(10), Push(20), Push(30), Push(40), Push(50) → 5 pushes. 2. Count pops: Pop(), Pop(), Pop(), Pop() → 4 pops. 3. Elements remaining = pushes − pops = 5 − 4 = 1. 4. (Trace: push10 → pop → empty; push20,push30 → pop → {20}; push40 → pop → {20}; pop → empty; push50 → {50}. So 1 element.)
Q125.Easy
A stack named 'tiffinBox' currently holds the elements [idli, vada] with 'vada' on top. If we perform Push(tiffinBox, samosa) and then Top(tiffinBox), what value is returned?
  1. samosa
  2. idli
  3. vada
  4. idli+vada
Show answer & explanation
Answer: (A) samosa
Explanation: 1. Push(tiffinBox, samosa) places samosa on top of the stack. 2. Stack becomes [idli, vada, samosa] with samosa at the top. 3. Top() returns the last inserted element, which is samosa.
Q126.Easy
While using the Stack ADT in Java, what happens if you call pop() on a stack that has no elements?
  1. An undefined error occurs
  2. The compiler displays a warning
  3. An EmptyStackException is thrown
  4. A NoStackException is thrown
Show answer & explanation
Answer: (C) An EmptyStackException is thrown
Explanation: 1. The Java Stack ADT checks whether the stack is empty before a pop(). 2. If the stack is empty, it cannot remove any element. 3. In that case it throws an EmptyStackException at run-time. 4. So calling pop() on an empty stack results in EmptyStackException.
Q127.Easy
A stack is implemented using a linked list where 'top' points to the top node. What does the following method do?
public Object peekElement() throws EmptyStackException
{
if(isEmpty())
throw new EmptyStackException("underflow");
return top.getData();
}
  1. removes the top element of the stack
  2. deletes the top element without returning it
  3. returns the top element of the stack without removing it
  4. inserts a new element on top of the stack
Show answer & explanation
Answer: (C) returns the top element of the stack without removing it
Explanation: 1. If the stack is empty, it throws an underflow exception. 2. Otherwise it simply returns the data stored in the top node. 3. The top pointer is not changed, so the element stays on the stack. 4. Hence it only retrieves (peeks at) the top element; it is not a pop.
Q128.Easy
Consider these stack functions:
push() : push an element onto the stack
pop() : remove the top element of the stack
top() : return the element at the top of the stack
What will be the output of the following sequence of operations?
push(15);
push(7);
top();
pop();
pop();
push(9);
top();
  1. 15
  2. 7
  3. stack underflow
  4. 9
Show answer & explanation
Answer: (D) 9
Explanation: Step 1: push(15) makes stack [15]. Step 2: push(7) makes stack [15, 7]; top() returns 7. Step 3: First pop() removes 7, leaving [15]. Step 4: Second pop() removes 15, leaving the stack empty. Step 5: push(9) makes stack [9]. Step 6: top() now returns 9.
Q129.Medium
In the following C function meant to push an element onto a stack, which line contains an error?
1. void push(struct stack *st, int val)
2. {
3. if(st->top == MAX-1)
4. {
5. printf("stack overflow");
6. }
7. else
8. {
9. st->items[++st->top] = val;
10. st++;
11. }
12. }
  1. Line 1
  2. Line 9
  3. Line 10
  4. Line 11
Show answer & explanation
Answer: (C) Line 10
Explanation: Line 9 correctly increments top and stores val, so the push itself is fine. Line 10 does st++, which wrongly advances the stack base pointer and corrupts memory on the next push. Hence the error is on line 10.
Q130.MediumAnswer under review
A stack contains the elements 7, 6, 5, 4 with 4 at the top. You want to transform it to a stack with elements 8, 7, 6, 5, 4 with 4 at the top. You are allowed only push and pop operations. Which sequence of operations will achieve this?
  1. Push(pop()), push(8), push(pop())
  2. Push(pop()), push(8)
  3. Push(pop()), push(pop()), push(8)
  4. Push(8)
Show answer & explanation
Answer: (A) Push(pop()), push(8), push(pop())
Explanation: To insert 8 at the bottom of the stack, we first transfer all elements to another stack using push(pop()) repeatedly, which reverses their order. Then we push 8 onto the now-empty original stack. Finally, we transfer the elements back using push(pop()) again, restoring the original order with 8 at the bottom. Thus, the sequence is: push(pop()) for each element, push(8), then push(pop()) for each element from the temporary stack.
Q131.Medium
What is the output of the following Java program?
class Pile {
  static final int LIMIT = 50;
  int top = -1;
  Object[] arr = new Object[LIMIT];
  void push(Object item) {
    if (top == LIMIT - 1) { System.out.println("Pile full"); return; }
    arr[++top] = item;
  }
  Object pop() {
    if (top < 0) return -1;
    Object e = arr[top];
    top--;
    return e;
  }
}
public class PileDemo {
  public static void main(String[] args) {
    Pile p = new Pile();
    p.push(25);
    Object a = p.pop();
    Object b = p.pop();
    System.out.println(b);
  }
}
  1. Pile full
  2. 25
  3. 0
  4. -1
Show answer & explanation
Answer: (D) -1
Explanation: 1) push(25) puts 25 on the pile, top becomes 0. 2) First pop() returns 25 and top becomes -1. 3) Second pop() finds top < 0 (empty pile) and returns -1. 4) So b = -1 and the program prints -1.

Stack using Queues

Q132.MediumAnswer under review
To implement a stack using queues that support only enqueue and dequeue operations, what is the minimum number of queues required?
  1. 1
  2. 2
  3. 3
  4. 4
Show answer & explanation
Answer: (B) 2
Explanation: Step 1: A stack is LIFO, but a queue is FIFO, so order must be reversed. Step 2: Either push or pop becomes a costly operation. Step 3: The costly operation needs a second queue to rearrange elements. Hence a minimum of 2 queues is required.
Q133.MediumAnswer under review
A queue is implemented using two stacks, S1 and S2. An element is said to be 'touched' whenever an operation (push or pop) is performed on it. During a dequeue operation, each element of the queue is moved from S1 to S2, the front element is removed from S2, and then all remaining elements are moved back from S2 to S1. How many times is each element touched during one such dequeue operation?
  1. Once
  2. Twice
  3. Thrice
  4. Four times
Show answer & explanation
Answer: (D) Four times
Explanation: Step 1: Each element is popped from S1 (touch 1). Step 2: Each element is pushed into S2 (touch 2). Step 3: After removing the front element, each remaining element is popped from S2 (touch 3). Step 4: Each element is pushed back into S1 (touch 4). So every element is touched four times.
Q134.Medium
Implementing stack operations using queues is not practical for a large dataset (with n elements in the stack). Why?
  1. Because the time complexity is O(n)
  2. Because the time complexity is O(log n)
  3. Because no extra memory is required
  4. There is no problem at all
Show answer & explanation
Answer: (A) Because the time complexity is O(n)
Explanation: 1. To simulate a stack with queues, each push or pop requires moving all current elements from one queue to another. 2. Moving n elements takes O(n) time per operation. 3. It also needs an extra queue as auxiliary storage. 4. For large n this O(n) cost per operation is not feasible, so the correct reason is the O(n) time complexity.
Q135.Medium
What is the minimum number of queues needed to implement a stack?
  1. 3
  2. 4
  3. 1
  4. 2
Show answer & explanation
Answer: (C) 1
Explanation: 1. A stack can be built with a single queue plus a counter of elements. 2. To push, enqueue the element and increment the counter. 3. To pop, rotate the queue by moving (count-1) elements from front to rear, then dequeue the last element and decrement the counter. 4. Thus one queue is sufficient, so the minimum is 1.

Suffix Arrays

Q136.Easy
Which of the following statements about a suffix array of a string is FALSE?
  1. A suffix array is always sorted
  2. A suffix array is used in string matching problems
  3. A suffix array is always unsorted
  4. A suffix array contains all the suffixes of the given string
Show answer & explanation
Answer: (C) A suffix array is always unsorted
Explanation: 1. A suffix array lists all suffixes of a string. 2. These suffixes are stored in sorted (lexicographic) order, so it is always sorted. 3. Suffix arrays are widely used to solve string matching problems. 4. Therefore the claim that a suffix array is always unsorted is false.
Q137.MediumAnswer under review
The suffix array of the string "bananas" is:
  1. 5 3 1 0 4 2 6
  2. 5 1 3 0 4 2 6
  3. 5 3 1 4 0 2 6
  4. 5 3 0 1 4 2 6
Show answer & explanation
Answer: (A) 5 3 1 0 4 2 6
Explanation: 1) List suffixes with starting index: 0 bananas, 1 ananas, 2 nanas, 3 anas, 4 nas, 5 as, 6 s. 2) Sort alphabetically: as(5), ananas(1), anas(3), bananas(0), nas(4), nanas(2), s(6). 3) Suffix array stores only indices in sorted order: 5 3 1 0 4 2 6.
Q138.Medium
If a comparison-based sorting algorithm is used to construct a suffix array, what is the time complexity to construct the suffix array?
  1. O(n log n)
  2. O(n^2)
  3. O(n^2 log n)
  4. O(n^2) + O(log n)
Show answer & explanation
Answer: (C) O(n^2 log n)
Explanation: 1. A suffix array contains n suffixes, each of length up to n. 2. Comparison-based sorting requires O(n log n) comparisons on average. 3. Comparing two suffixes takes O(n) time in the worst case. 4. Total time = O(n log n) comparisons × O(n) per comparison = O(n^2 log n).
Q139.Medium
What will be the suffix array of the string "banana"?
  1. 5 3 1 0 4 2
  2. 5 3 1 0 2 4
  3. 0 1 2 3 4 5
  4. 5 3 0 1 4 2
Show answer & explanation
Answer: (A) 5 3 1 0 4 2
Explanation: 1) List all suffixes of "banana" with their starting indices: 0:banana, 1:anana, 2:nana, 3:ana, 4:na, 5:a. 2) Sort the suffixes alphabetically: a (5), ana (3), anana (1), banana (0), na (4), nana (2). 3) The suffix array is the list of starting indices in sorted order: 5 3 1 0 4 2.

Time Complexity

Q140.Easy
What is the time complexity of the following code?
public boolean isBalanced(String exp)
{
  int len = exp.length();
  Stack<Integer> stk = new Stack<Integer>();
  for(int i = 0; i < len; i++)
  {
    char ch = exp.charAt(i);
    if (ch == '(')
      stk.push(i);
    else if (ch == ')')
    {
      if(stk.isEmpty())
        return false;
      stk.pop();
    }
  }
  return stk.isEmpty();
}
  1. O(log n)
  2. O(n)
  3. O(1)
  4. O(n log n)
Show answer & explanation
Answer: (B) O(n)
Explanation: Step 1: The loop runs once for each of the n characters of the string. Step 2: Inside the loop, push, pop and isEmpty each take O(1) time. Step 3: Total work = n × O(1) = O(n). Step 4: So the time complexity is O(n).
Q141.Easy
What is the worst-case time complexity of inserting a node at an arbitrary given position in a doubly linked list of n nodes?
  1. O(n log n)
  2. O(log n)
  3. O(n)
  4. O(1)
Show answer & explanation
Answer: (C) O(n)
Explanation: Step 1: To insert at a given position, you must first reach that position by traversing the list from the head. Step 2: In the worst case the position is at the end, so all n nodes are visited. Step 3: The pointer updates after reaching the position take O(1). Step 4: Total worst-case cost is therefore O(n).
Q142.Easy
What is the time complexity to count the number of elements in a singly linked list?
  1. O(1)
  2. O(n)
  3. O(log n)
  4. O(n^2)
Show answer & explanation
Answer: (B) O(n)
Explanation: 1. A linked list does not store its length; nodes are reachable only by following next pointers. 2. To count elements you must visit every node once, starting from the head. 3. Visiting n nodes takes time proportional to n. 4. Hence the time complexity is O(n).
Q143.Easy
A singly linked list has n nodes and the only pointer available initially points to the head node. What is the asymptotic time complexity of inserting a new node at the END of this list?
  1. O(1)
  2. O(n)
  3. O(log n)
  4. O(n log n)
Show answer & explanation
Answer: (B) O(n)
Explanation: 1. The head pointer gives direct access only to the first node. 2. To insert at the end we must first walk from head through every node to reach the last node. 3. That traversal touches all n nodes, so the work grows linearly with n. 4. Hence the time complexity is O(n).

Other questions

Q144.Easy
Which of the following statements about prefix and postfix operators is INCORRECT?
  1. Postfix operators use the value to their right
  2. Postfix operators use the value to their left
  3. Prefix operators use the value to their right
  4. In a postfix expression, operands are followed by operators
Show answer & explanation
Answer: (A) Postfix operators use the value to their right
Explanation: 1) In postfix notation the operator comes after its operands, so it acts on values to its left. 2) In prefix notation the operator comes before its operands, so it acts on values to its right. 3) Therefore saying a postfix operator uses the value to its right is wrong. 4) That incorrect statement is the answer.
Q145.Easy
In the infix to postfix conversion algorithm, how are operators of the same precedence associated?
  1. right to left
  2. left to right
  3. centre to left
  4. centre to right
Show answer & explanation
Answer: (B) left to right
Explanation: Step 1: In infix notation, operators of equal precedence are evaluated left to right. Step 2: The conversion algorithm respects this, popping operators of equal precedence before pushing a new one. Hence operators are associated left to right.
Q146.Easy
The exponentiation operator ^ is right-associative.
  1. True
  2. False
Show answer & explanation
Answer: (A) True
Explanation: Step 1: For ^, a^b^c is evaluated as a^(b^c), i.e. from the right. Step 2: Operators like +,-,*,/ are left-associative. Step 3: Hence the statement is True.
Q147.EasyAnswer under review
An expression tree has root node '+'. Its left child is a leaf node '6'. Its right child is a node '*', whose left child is a leaf node '3' and right child is a leaf node '4'. Write the infix expression represented by this tree and evaluate it.
  1. 14
  2. 18
  3. 24
  4. 10
Show answer & explanation
Answer: (A) 14
Explanation: Step 1: The right subtree is (3*4). Step 2: The root '+' combines left leaf 6 with right subtree: 6+(3*4). Step 3: Evaluate 3*4 = 12. Step 4: 6+12 = 18? Wait, recheck: 6+12 = 18. Correct value is 18. Step 5: So the infix expression is 6+(3*4) = 18.
Q148.Easy
An expression tree has '+' at the root, with left child 'x' and right child '*'. The '*' node has left child 'y' and right child 'z'. What is the correct postfix expression for this tree?
  1. xyz*+
  2. xyz+*
  3. xy+z*
  4. x+yz*
Show answer & explanation
Answer: (A) xyz*+
Explanation: 1. The left subtree of the root is just 'x', so its postfix is 'x'. 2. The right subtree is '*', with children 'y' and 'z'. Its postfix is 'yz*'. 3. The root is '+', so the overall postfix is left, then right, then root: 'x' + 'yz*' + '+' = 'xyz*+'.
Q149.Easy
Which of the following is an infix expression?
  1. (x+y)*(z+w)
  2. xy+z*
  3. +xy
  4. xyz+*
Show answer & explanation
Answer: (A) (x+y)*(z+w)
Explanation: Step 1: Infix places the operator between operands, e.g. x+y. Step 2: (x+y)*(z+w) has every operator between its operands, so it is infix. Step 3: +xy is prefix and xy+z* / xyz+* are postfix.
Q150.Easy
Reverse Polish Notation (postfix) is simply the reverse of Polish Notation (prefix).
  1. True
  2. False
Show answer & explanation
Answer: (B) False
Explanation: Step 1: Prefix places the operator before its operands; postfix places it after. Step 2: Reversing a prefix string does not generally yield a valid postfix expression (operand order also flips). Step 3: Both are read left to right but use different strategies, so the statement is False.
Q151.Easy
What is the prefix (Polish) notation for the expression (P*Q)+(R*S)?
  1. +*PQ*RS
  2. *+PQ*RS
  3. PQ+RS
  4. +*QP*RS
Show answer & explanation
Answer: (A) +*PQ*RS
Explanation: Step 1: The main operator is +, joining (P*Q) and (R*S). Step 2: Prefix of (P*Q) is *PQ. Step 3: Prefix of (R*S) is *RS. Step 4: Combine: + *PQ *RS = +*PQ*RS.
Q152.Easy
Which predefined method in Java is used to convert a decimal integer into its binary string representation?
  1. toBinaryInteger(int)
  2. toBinaryValue(int)
  3. toBinaryNumber(int)
  4. toBinaryString(int)
Show answer & explanation
Answer: (D) toBinaryString(int)
Explanation: Step 1: Java's Integer class provides static conversion methods. Step 2: The correct method is Integer.toBinaryString(int). Step 3: It returns the string form of the unsigned binary value of the argument, e.g. Integer.toBinaryString(10) gives "1010". Step 4: The other three method names do not exist in Java.
Q153.Easy
In the standard algorithm that converts an infix expression to postfix form, how many types of input characters are accepted?
  1. one
  2. two
  3. three
  4. four
Show answer & explanation
Answer: (C) three
Explanation: Step 1: The algorithm reads operands (numbers/variables). Step 2: It reads operators (+, -, *, /, etc.). Step 3: It also reads a newline/end-of-expression character to know when input ends. Step 4: So three kinds of input characters are accepted.
Q154.Easy
What is the time complexity of converting a decimal number into its binary representation by repeated division by 2?
  1. O(1)
  2. O(n)
  3. O(log n)
  4. O(n log n)
Show answer & explanation
Answer: (C) O(log n)
Explanation: Step 1: Each division by 2 reduces the number roughly by half. Step 2: The number of divisions needed is about log2(n). Step 3: Hence the time complexity is O(log n).
Q155.EasyAnswer under review
What decides the order in which the operators of a prefix expression are applied during evaluation?
  1. precedence and associativity
  2. precedence only
  3. associativity only
  4. it depends on the parser used
Show answer & explanation
Answer: (A) precedence and associativity
Explanation: 1) Precedence decides which operator binds more tightly, so it is a key factor in the order of evaluation. 2) When two operators have the same precedence, associativity decides the order. 3) Therefore both precedence and associativity together determine the evaluation order.
Q156.Easy
Among the operators (+, %, ^, &, #), which one has the highest priority in the standard infix-to-prefix conversion algorithm?
  1. +
  2. #
  3. ^
  4. &
Show answer & explanation
Answer: (C) ^
Explanation: 1) In the standard precedence order used for infix-to-prefix conversion, exponentiation '^' has the highest priority. 2) Multiplicative operators like '%' come next, then additive operators like '+'. 3) The symbols '&' and '#' are not standard arithmetic operators and are given lower/placeholder priority. 4) Hence '^' has the highest priority.
Q157.Easy
In the standard infix-to-postfix conversion algorithm, which of the following groups of operators has the highest precedence?
  1. '(' and ')'
  2. '*' and '/'
  3. '^' and unary '~'
  4. '+' and '-'
Show answer & explanation
Answer: (C) '^' and unary '~'
Explanation: 1) In the standard precedence table, exponentiation '^' and unary operators like '~' sit at the top. 2) Next come '*' and '/', then '+' and '-', and parentheses are handled separately as delimiters, not as ordinary operators. 3) Hence among the given groups, '^' and unary '~' have the highest precedence.
Q158.Easy
Among the operators (|, *, +, &, $), the one having the lowest priority is
  1. +
  2. $
  3. |
  4. &
Show answer & explanation
Answer: (C) |
Explanation: 1. Standard precedence: unary/postfix highest, then *, then +, then bitwise &, then logical |. 2. Logical OR (|) has the lowest precedence among these. 3. Hence | is the lowest-priority operator.
Q159.Easy
Among the operators ^, +, /, and #, which one has the lowest precedence (where ^ is exponentiation, and # is a custom operator with the lowest precedence)?
  1. ^
  2. +
  3. /
  4. #
Show answer & explanation
Answer: (D) #
Explanation: Step 1: Standard precedence high→low: ^ (exponent) > / (division) > + (addition). Step 2: The custom operator # is defined to have the lowest precedence of all. Step 3: Therefore # is the lowest-precedence operator.
Q160.Easy
Which statement best describes an array in data structures?
  1. A data structure that arranges data in a hierarchical (tree-like) manner
  2. A container that holds a collection of elements of the same data type
  3. A structure whose elements cannot be modified after it is created
  4. An array is not considered a data structure at all
Show answer & explanation
Answer: (B) A container that holds a collection of elements of the same data type
Explanation: 1) An array stores many values under one name. 2) All these values must be of the same type (e.g., all integers). 3) They are kept in contiguous memory locations. 4) So the best description is a container of elements of similar type.
Q161.Easy
Ravi is designing a program where he declares an integer array of size 50, but only stores 20 marks in it. Which of the following is a genuine disadvantage of using arrays in this situation?
  1. Structures like stacks and queues cannot be built using arrays
  2. Memory is wasted because the unused positions of the fixed-size array cannot be reclaimed
  3. The index of an array element can be a negative number
  4. Array elements can only be accessed one after another, never directly
Show answer & explanation
Answer: (B) Memory is wasted because the unused positions of the fixed-size array cannot be reclaimed
Explanation: 1) An array is allocated with a fixed size at declaration, here 50 slots. 2) Only 20 slots are used, so 30 slots stay empty. 3) This unused space cannot be given back or reused for other data. 4) Hence the real disadvantage is wastage of memory due to fixed size.
Q162.Easy
Assuming an int occupies 4 bytes, what is the total size in bytes of the array declared as int arr[20];?
  1. 20
  2. 24
  3. 80
  4. 16
Show answer & explanation
Answer: (C) 80
Explanation: Step 1: The array int arr[20] holds 20 integer elements. Step 2: Each int takes 4 bytes of memory. Step 3: Total size = number of elements x size of one element = 20 x 4. Step 4: 20 x 4 = 80 bytes. Step 5: So the array occupies 80 bytes.
Q163.Easy
How are elements of an array accessed?
  1. randomly (directly by index)
  2. sequentially (one after another)
  3. exponentially
  4. logarithmically
Show answer & explanation
Answer: (A) randomly (directly by index)
Explanation: Step 1: An array stores its elements in contiguous memory locations. Step 2: The address of any element can be computed directly from its index. Step 3: So any element can be reached in constant time, i.e. random (direct) access. Step 4: Linked lists, in contrast, must be traversed node by node, which is sequential access. Step 5: Therefore array elements are accessed randomly.
Q164.Easy
Which of the following correctly initializes an integer array of 3 elements in C with values 4, 5 and 6?
  1. int nums[3] = (4,5,6);
  2. int nums(3) = {4,5,6};
  3. int nums[3] = {4,5,6};
  4. int nums(3) = (4,5,6);
Show answer & explanation
Answer: (C) int nums[3] = {4,5,6};
Explanation: Step 1: In C, the size of an array is written inside square brackets [ ]. Step 2: The initial values are enclosed in curly braces { } and separated by commas. Step 3: So int nums[3] = {4,5,6}; stores 4, 5, 6 in nums[0], nums[1], nums[2]. Step 4: Parentheses ( ) are not used for array size or initializer lists in C. Step 5: Hence the correct syntax is int nums[3] = {4,5,6};
Q165.Easy
In Java, which of the following correctly creates (instantiates) an integer array of 5 elements?
  1. int marks[] = new int(5);
  2. int marks[];
  3. int marks[] = new int[5];
  4. int marks() = new int(5);
Show answer & explanation
Answer: (C) int marks[] = new int[5];
Explanation: Step 1: In Java, arrays are objects created with the new keyword. Step 2: The size of an array is given in square brackets [ ], not parentheses ( ). Step 3: So new int[5] allocates space for 5 integers. Step 4: 'int marks[];' only declares a reference, it does not create the array. Step 5: Hence the correct instantiation is int marks[] = new int[5];
Q166.Easy
Which of the following is the correct way to declare a multidimensional array in Java?
  1. int[] arr;
  2. int arr[[]];
  3. int[][] arr;
  4. int[[]] arr;
Show answer & explanation
Answer: (C) int[][] arr;
Explanation: 1. A multidimensional (2-D) array in Java is declared with two pairs of square brackets. 2. The brackets may come before the variable name: int[][] arr; or after it: int arr[][]; 3. int[] arr; declares only a 1-D array. 4. int arr[[]]; and int[[]] arr; are invalid syntax. 5. Hence int[][] arr; is correct.
Q167.Easy
When does the ArrayIndexOutOfBoundsException occur in Java?
  1. At compile-time
  2. At run-time
  3. It is not an error
  4. It is not an exception at all
Show answer & explanation
Answer: (B) At run-time
Explanation: 1. The compiler only checks syntax and types, not the actual index value used. 2. The index is checked while the program executes. 3. If the index is outside 0 to length-1, the JVM throws ArrayIndexOutOfBoundsException at run-time. 4. Hence it is a run-time exception, and compilation is error-free.
Q168.Easy
Which of the following is an advantage of using arrays?
  1. Objects of different data types can be stored together
  2. Elements of an array cannot be sorted
  3. The index of the first element of an array is 1
  4. It is easier to store many elements of the same data type
Show answer & explanation
Answer: (D) It is easier to store many elements of the same data type
Explanation: 1) An array holds many elements of the same data type in contiguous memory. 2) This makes storing and accessing a large collection of similar values easy. 3) Mixed data types are not allowed, arrays can be sorted, and the first index is 0. 4) Hence the advantage is storing many same-type elements easily.
Q169.Easy
In C, for int prices[12]; what does sizeof(prices) return (assuming int is 4 bytes)?
  1. 12
  2. 48
  3. The address of the array
  4. The size of a pointer (8 bytes)
Show answer & explanation
Answer: (B) 48
Explanation: 1. sizeof(array) gives the total bytes occupied by the whole array. 2. The array has 12 elements. 3. Each int is 4 bytes, so total = 12 × 4 = 48 bytes. 4. Hence sizeof(prices) returns 48.
Q170.Easy
In C, when an array is passed as an argument to a function, what is actually passed?
  1. A full copy of the entire array
  2. The base address of the array
  3. Only the size of the array
  4. The last element of the array
Show answer & explanation
Answer: (B) The base address of the array
Explanation: 1. In C, an array name used as a function argument decays into a pointer to its first element. 2. So the function receives the base address (address of element 0), not a copy. 3. Changes made through that pointer affect the original array.
Q171.Easy
In C, which of the following situations typically causes a segmentation fault?
  1. Reading a variable that was properly initialized
  2. Using an uninitialized variable in arithmetic
  3. Dereferencing a pointer to an invalid memory location
  4. Passing a pointer to a function
Show answer & explanation
Answer: (C) Dereferencing a pointer to an invalid memory location
Explanation: 1. A segmentation fault occurs when a program tries to access memory it is not allowed to access. 2. Dereferencing a pointer that holds an invalid or unallocated address does exactly that. 3. Reading initialized variables, doing arithmetic on uninitialized values, or passing a valid pointer to a function do not by themselves access illegal memory.
Q172.Easy
In C, if int marks[4] = {10, 20, 30, 40}; what happens when you execute printf("%d", marks[7]); ?
  1. Compile-time error
  2. Always a runtime error
  3. Undefined behaviour (garbage value or crash)
  4. It prints 0
Show answer & explanation
Answer: (C) Undefined behaviour (garbage value or crash)
Explanation: 1. The array marks has valid indices 0 to 3 only. 2. Index 7 is outside the allocated bounds. 3. C does not check array bounds at compile time or runtime. 4. Accessing marks[7] reads memory beyond the array, which is undefined behaviour: it may print a garbage value or crash.
Q173.Easy
Which tool is commonly used to detect array out-of-bounds errors in C programs at runtime?
  1. printf
  2. Compiler only
  3. AddressSanitizer
  4. Preprocessor
Show answer & explanation
Answer: (C) AddressSanitizer
Explanation: 1. printf is for output, not error detection. 2. The compiler can catch some errors at compile time but not runtime out-of-bounds. 3. AddressSanitizer is a runtime tool that detects memory errors including out-of-bounds accesses. 4. The preprocessor handles macros and includes, not runtime errors. 5. Therefore, AddressSanitizer is the correct tool.
Q174.Easy
What is the key difference between a circular linked list and a normal (linear) linked list?
  1. In a circular linked list, the next pointer of the last node points back to the first node, whereas in a normal linked list it is null.
  2. A circular linked list allows faster traversal than a normal linked list.
  3. In a circular linked list, the next pointer may or may not be null.
  4. A circular linked list always has a known head node, while a normal linked list does not.
Show answer & explanation
Answer: (A) In a circular linked list, the next pointer of the last node points back to the first node, whereas in a normal linked list it is null.
Explanation: 1. In a normal singly linked list, the last node's next pointer is null, indicating the end. 2. In a circular linked list, the last node's next pointer points back to the head (or any node), forming a loop. 3. This means there is no null pointer at the end, so traversal can continue indefinitely unless stopped. 4. Therefore, the main difference is the next pointer of the last node.
Q175.Easy
What is the worst-case time complexity of searching for an element in a circular linked list of n nodes?
  1. O(n)
  2. O(n log n)
  3. O(1)
  4. O(n^2)
Show answer & explanation
Answer: (A) O(n)
Explanation: 1. To search, you start at some node and follow next pointers around the circle. 2. In the worst case the element is the last one visited (or absent), so you must check all n nodes. 3. Therefore the worst-case time complexity is O(n).
Q176.Easy
Which of the following applications is best implemented using a circular linked list?
  1. Undo operation in a text editor
  2. Recursive function calls
  3. Round-robin allocation of CPU time to processes
  4. Implementing hash tables
Show answer & explanation
Answer: (C) Round-robin allocation of CPU time to processes
Explanation: 1. Round-robin scheduling cycles through processes repeatedly, which maps naturally to a circular linked list where the last node points back to the first. 2. Undo operations need traversal in both directions, so a doubly linked list is used. 3. Recursive calls use the stack data structure. 4. Hash tables typically use singly linked lists for chaining. 5. Hence round-robin CPU allocation is the correct application.
Q177.Easy
Which of the following statements about a doubly linked list is FALSE?
  1. We can traverse the list in both directions
  2. It needs more memory per node than a singly linked list
  3. Insertion and deletion of a node take slightly longer
  4. Implementing a doubly linked list is easier than implementing a singly linked list
Show answer & explanation
Answer: (D) Implementing a doubly linked list is easier than implementing a singly linked list
Explanation: 1. A doubly linked list has both prev and next pointers, so traversal in both directions is possible — statement 1 is true. 2. The extra pointer means more memory per node than a singly linked list — statement 2 is true. 3. Every insert/delete must update two pointers instead of one, so it takes a bit longer — statement 3 is true. 4. Implementation is harder, not easier, because both prev and next links must be maintained correctly — so statement 4 is false.
Q178.Easy
What is a memory-efficient doubly linked list?
  1. Each node stores only one pointer field, yet the list can still be traversed in both directions
  2. The list stores extra breakpoints so traversal becomes faster
  3. A separate helper singly linked list is used to walk through the doubly linked list
  4. A doubly linked list that stores addresses using the bitwise AND operator
Show answer & explanation
Answer: (A) Each node stores only one pointer field, yet the list can still be traversed in both directions
Explanation: 1. A normal doubly linked list needs two pointer fields per node (prev and next), which costs extra memory. 2. The memory-efficient version keeps just one field per node. 3. That field holds the XOR of the previous and next node addresses. 4. During traversal, XORing the stored value with the address of the node you came from gives the next address, so you can move forward or backward. 5. Thus one pointer field is enough to traverse in both directions.
Q179.Easy
A linear collection of elements in which removal happens only at one end (called the front) and addition happens only at the other end (called the rear) is known as a:
  1. Queue
  2. Stack
  3. Binary tree
  4. Linked list
Show answer & explanation
Answer: (A) Queue
Explanation: Step 1: In this structure, elements are added at the rear and removed from the front. Step 2: This first-in-first-out (FIFO) behaviour is exactly the definition of a queue. Step 3: A stack removes the most recently added element (LIFO), so it does not match. Step 4: Trees and linked lists are not restricted to add-at-one-end/remove-at-other-end. Therefore the answer is Queue.
Q180.Easy
Which of the following properties is associated with a queue data structure?
  1. First In Last Out
  2. First In First Out
  3. Last In First Out
  4. Last In Last Out
Show answer & explanation
Answer: (B) First In First Out
Explanation: Step 1: In a queue, insertion happens at the rear and deletion at the front. Step 2: The element inserted first is removed first. Step 3: Therefore a queue follows the First In First Out (FIFO) order.
Q181.Easy
Which data structure is required to perform Breadth First Traversal (BFS) on a graph?
  1. Stack
  2. Array
  3. Queue
  4. Tree
Show answer & explanation
Answer: (C) Queue
Explanation: 1. In BFS, we start from a vertex and visit all its unvisited adjacent vertices. 2. These adjacent vertices must be processed in the order they were discovered, i.e. the earliest discovered vertex is handled first. 3. This requires the First In First Out (FIFO) principle. 4. A queue follows FIFO, so it is the correct structure (a stack would give DFS). So the answer is Queue.
Q182.Easy
A queue operates on which principle?
  1. FIFO (First In First Out) principle
  2. LIFO (Last In First Out) principle
  3. Ordered array principle
  4. Linear tree principle
Show answer & explanation
Answer: (A) FIFO (First In First Out) principle
Explanation: 1. In a queue, elements are added at the rear and removed from the front. 2. The element that was inserted first is the one removed first. 3. This is exactly the First In First Out (FIFO) principle. LIFO belongs to a stack, and the other options are not queue principles. So the answer is FIFO.
Q183.Easy
Which data structure allows insertion and deletion of elements at both the front and the rear ends, but not in the middle?
  1. Queue
  2. Circular queue
  3. Deque
  4. Priority queue
Show answer & explanation
Answer: (C) Deque
Explanation: 1. A queue allows insertion at the rear and deletion at the front only. 2. A circular queue also restricts operations to the two ends in FIFO order. 3. A priority queue removes the element with the highest (or lowest) priority, not by end position. 4. A deque (double-ended queue) permits insertion and deletion at both the front and the rear, but not in the middle. So the answer is Deque.
Q184.Easy
What is a deque (double-ended queue)?
  1. A queue in which insertion and deletion are allowed at both the front and the rear ends
  2. A queue implemented using a doubly linked list
  3. A queue implemented using both singly and doubly linked lists
  4. A queue in which insertion and deletion are allowed only at the front end
Show answer & explanation
Answer: (A) A queue in which insertion and deletion are allowed at both the front and the rear ends
Explanation: 1) A normal queue allows insertion at the rear and deletion at the front only. 2) A deque (double-ended queue) relaxes this: both ends support insert and delete. 3) The implementation detail (array, singly or doubly linked list) does not define a deque. 4) So the correct definition is insert/delete allowed at both front and rear.
Q185.Easy
A normal (non-circular) queue is implemented using an array of size MAX_SIZE. The queue becomes full when:
  1. Rear = MAX_SIZE - 1
  2. Front = (rear + 1) mod MAX_SIZE
  3. Front = rear + 1
  4. Rear = front
Show answer & explanation
Answer: (A) Rear = MAX_SIZE - 1
Explanation: 1) In a normal array-based queue, elements are added at the rear and the rear index increases by 1 each time. 2) Valid indices run from 0 to MAX_SIZE - 1. 3) When rear reaches MAX_SIZE - 1, no further index is available for insertion. 4) Therefore the queue is full when rear = MAX_SIZE - 1.
Q186.Easy
Which of the following is NOT a type of queue?
  1. Ordinary queue
  2. Single-ended queue
  3. Circular queue
  4. Priority queue
Show answer & explanation
Answer: (B) Single-ended queue
Explanation: 1) A queue by definition has two ends: one for insertion and one for deletion. 2) Ordinary, circular and priority queues are all standard queue variants. 3) A 'single-ended queue' would have only one usable end, which contradicts the queue concept. 4) Hence single-ended queue is not a valid type of queue.
Q187.Easy
A double-ended queue (deque) supports addFront, addRear, removeFront and removeRear. You are allowed to use only stacks with push and pop operations, and stacks may be reused. What is the minimum number of stacks needed to implement all four deque operations?
  1. 1
  2. 2
  3. 3
  4. 4
Show answer & explanation
Answer: (B) 2
Explanation: Step 1: addFront and removeFront work directly on one stack using push and pop. Step 2: For addRear and removeRear, elements must be reversed, which needs a second stack. Step 3: Pop all elements from the first stack into the second, perform the rear operation, then move them back. Hence a minimum of 2 stacks is required.
Q188.Easy
In a queue implemented with a singly linked list, where is a newly enqueued element inserted?
  1. At the head of the linked list
  2. At the middle of the linked list
  3. At the tail of the linked list
  4. At any position in the linked list
Show answer & explanation
Answer: (C) At the tail of the linked list
Explanation: 1. A queue follows FIFO order. 2. New elements join at the rear (tail) so they leave last. 3. Hence insertion happens at the tail of the linked list.
Q189.Easy
Which of the following is NOT a typical application of a priority queue?
  1. Huffman coding
  2. Handling interrupts in an operating system
  3. Undo operation in a text editor
  4. Bayesian spam filtering
Show answer & explanation
Answer: (C) Undo operation in a text editor
Explanation: 1. A priority queue serves items by priority, used in Huffman coding, interrupt handling, and spam filtering. 2. Undo in editors needs the most recent action first (LIFO), which is a stack. 3. So undo is not a priority queue application.
Q190.Easy
A queue is implemented using a linked list where both 'front' and 'rear' pointers are maintained. During an insertion (enqueue) into a NON-EMPTY queue, which pointer(s) will change?
  1. Only the front pointer
  2. Only the rear pointer
  3. Both the front and rear pointers
  4. No pointer will change
Show answer & explanation
Answer: (B) Only the rear pointer
Explanation: 1. A queue follows FIFO order, so a new element is always added at the rear. 2. Since the queue is non-empty, the front element stays the same, so 'front' does not move. 3. The new node is linked after the current last node and 'rear' is updated to point to it. 4. Therefore only the rear pointer changes.
Q191.Easy
In a queue implemented with a fixed-size array, what is the condition called when you try to insert an element but the queue is already full?
  1. Overflow
  2. Underflow
  3. Null reference error
  4. Compilation failure
Show answer & explanation
Answer: (A) Overflow
Explanation: 1. A queue has a fixed capacity when implemented using an array. 2. Insertion (enqueue) adds an element at the rear. 3. If the queue already holds CAPACITY elements, there is no space for a new element. 4. This situation of inserting into a full queue is termed overflow. 5. (Removing from an empty queue, by contrast, is underflow.)
Q192.Easy
Which of the following is an application of a deque (double-ended queue)?
  1. Work-stealing scheduling algorithm
  2. Implementation of both stack and queue
  3. Finding the maximum in all subarrays of size k
  4. All of the above
Show answer & explanation
Answer: (D) All of the above
Explanation: A deque can be used in work-stealing scheduling (where tasks are taken from both ends), can simulate both stack and queue operations, and is used in the sliding window maximum problem to efficiently find the maximum in all subarrays of size k. Hence, all listed are applications.
Q193.Easy
In a linked-list implementation of a queue, what is the key condition that indicates the queue is empty?
  1. The front pointer is null
  2. The rear pointer is null
  3. The link field of the front node is empty
  4. front equals rear minus one
Show answer & explanation
Answer: (A) The front pointer is null
Explanation: 1. In a linked queue, nodes are removed from the front and added at the rear. 2. When no nodes remain, the front pointer has nothing to point to, so it becomes null. 3. Hence the queue is empty exactly when front is null (rear is also null then). 4. So the correct condition is 'front is null'.
Q194.Easy
A deque is implemented using a singly linked list where only the head pointer is maintained. What is the time complexity of deleting an element from the rear end of this deque?
  1. O(n log n)
  2. O(log n)
  3. O(n)
  4. O(n^2)
Show answer & explanation
Answer: (C) O(n)
Explanation: With only a head pointer, to reach the rear (last node) you must traverse all n nodes from the head. Hence deleting from the rear takes O(n) time.
Q195.Easy
A ticket counter at a railway station uses a simple linear queue that can hold up to n passengers' tokens at a time. What is the space complexity of this linear queue with respect to n?
  1. O(n)
  2. O(n log n)
  3. O(log n)
  4. O(1)
Show answer & explanation
Answer: (A) O(n)
Explanation: 1. Space complexity counts total memory used, including input storage. 2. A linear queue stores each of its n elements in its own slot. 3. So memory grows in direct proportion to n. 4. Hence the space complexity is O(n).
Q196.Easy
What is the auxiliary space complexity of deleting (freeing) all nodes of a singly linked list iteratively?
  1. O(1)
  2. O(n)
  3. Either O(1) or O(n)
  4. O(log n)
Show answer & explanation
Answer: (A) O(1)
Explanation: 1. Iterative deletion uses only a couple of temporary pointers (current and next). 2. The number of such variables does not grow with the number of nodes n. 3. Hence extra space is constant, O(1).
Q197.Easy
Which of the following is NOT a disadvantage of using an array?
  1. Fixed size decided in advance
  2. Memory may be wasted if fewer elements are stored than the allocated size
  3. Insertion at a given position requires shifting of elements
  4. Accessing the element at a specified position
Show answer & explanation
Answer: (D) Accessing the element at a specified position
Explanation: 1. Arrays have a fixed size, so they cannot grow or shrink at runtime — a disadvantage. 2. If the allocated size is larger than the number of elements used, memory is wasted — a disadvantage. 3. Inserting in the middle requires shifting later elements, costing O(n) time — a disadvantage. 4. Accessing an element at a given index is done in constant time using the base address, so it is an advantage, not a disadvantage.
Q198.Easy
Which data structure is best suited to quickly answer the question 'What is the item at position n?'
  1. Singly linked list
  2. Doubly linked list
  3. Circular linked list
  4. Array
Show answer & explanation
Answer: (D) Array
Explanation: 1. An array stores elements in contiguous memory, so the address of position n is base address + n × size of element. 2. This computation takes constant time, giving O(1) random access. 3. In singly, doubly or circular linked lists, you must walk node by node from the head, taking O(n) time. 4. Therefore an array is the best choice.
Q199.Easy
A linear collection of data elements in which each element stores the address of the next element is called a:
  1. Linked list
  2. Node list
  3. Primitive list
  4. Unordered list
Show answer & explanation
Answer: (A) Linked list
Explanation: 1. In a linked list, each node holds its own data plus a pointer to the next node. 2. The nodes are connected only through these pointers, forming a linear chain. 3. A 'node list' is a document concept, 'primitive list' is not a standard structure, and 'unordered list' is an HTML term. 4. Hence the correct term is linked list.
Q200.Easy
Which code correctly inserts a new node at the beginning of a singly linked list (head is the reference to the first node, size is the node count)?
  1. public void insertBegin(Node node) { node.setNext(head); head = node; size++; }
  2. public void insertBegin(Node node) { head = node; node.setNext(head); size++; }
  3. public void insertBegin(Node node) { Node temp = head.getNext(); node.setNext(temp); head = node; size++; }
  4. public void insertBegin(Node node) { Node temp = head.getNext(); node.setNext(temp); node = head; size++; }
Show answer & explanation
Answer: (A) public void insertBegin(Node node) { node.setNext(head); head = node; size++; }
Explanation: 1) The new node's next pointer must point to the current first node: node.setNext(head). 2) Then head must be updated to the new node: head = node. 3) Finally size is incremented. 4) Option b sets head first, losing the old list; options c and d skip the first node or never update head.
1234

Get In Touch

INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India

contact@intmatedtech.com

TestMyIndia All India Rank