MCQ on Data Structure – Stack and Queue

MCQ on stack and queue data structure. The concept of Stack and Queue data structure is very important in computer science. In my previous post i have discussed following things.

Stack program in C using Array.

Queue data structure and their implementation.

The purpose of this objective questions is to test how well you understand the concept of stack and queue.

Find the Second Largest Number in an Unsorted Array

Write a Program to find the second largest number in an unsorted array. Given an array of unsorted values find the second largest number in an array.

For example – Let’s take an array.

arr[] = { 4, 1, 5, 2, 8, 0, 3, 55}

The second largest element in this array is 8.  So how do you find the second largest number in an array ?  There are multiple ways by which you can solve this problem.

Find Second Largest Number in an Array

Find Second Largest Number in an Array

First Approach : Sort an Array

The simple approach is  to sort an array using sorting algorithm. After sorting an array element at n-2 index will be the second largest element.

You can use following sorting algorithms.

i) Selection Sort

ii) Insertion Sort

iii) Bubble Sort

iv) Merge Sort

v)Quick Sort

Selection sort, insertion sort and bubble sort are not suitable for large data sets as their time complexity is O(n2) . You can use Merge Sort or Quick Sort for large data sets.

NOTE –  If  values of an array is  repeated then this approach won’t work. Then the best approach is to traverse an array to find the second largest element.

Second Approach : Traverse an array

The best approach is to traverse an array to find the second largest number in an array. Using this approach maintain two flags one for highest and other for the second highest number. The time complexity of this approach is O(n).

Program to Delete a Linked List

Write a program to delete a linked list. Given a linked list, we have to write a code to delete a linked list.

To delete a complete linked list, traverse all the nodes of a linked list and delete one by one.

Print Middle Element of a Linked List

How to Delete a Linked List

To delete a node one by one maintain two pointers.  The first pointer points to head and the second pointer keeps the reference to next node. So when a node is free, you can assign the reference of next node using the second pointer.

Reverse a Linked List using Recursion

Write a program to reverse a linked list using recursion. Given a linked list print them in reverse order using recursion. This question is mostly asked in interviews.

In my last post, I have explained how to reverse a linked list using an iterative approach. In this post, We will learn how to reverse a linked list using recursion.  The time complexity for both iterative and recursive approach is O(n).

Print Fibonacci Series using Recursion in C, C++

Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

You can check my previous post to use iterative approach to print Fibonacci series.

What is Recursion.

In recursion function call itself until the base condition is reached.
Read explanation of recursion and what’s the difference between iterative and recursive function.

Objective Question on Recursion.