Questions
Questions
Set-1
1a. Write a Java program to implement a queue using two stacks. Include operations to enqueue, dequeue, check if the queue is empty,
and display the queue contents.
1b. Write a Java program to find the minimum element in a rotated sorted array using a modified binary search.
Set-2
2a. Write a Java program that, for a given array, generates all (value, minimum) pairs for each rotation of the array and counts the total
number of such pairs where value > minimum.
2b. Write a Java program to implement a stack using two queues. Include operations for push, pop, top, display, and check if the stack is
empty.
Set-3
3a. Write a Java program to remove duplicates from an unsorted linked list using a HashSet.
3b. Write a Java program to search an element in a rotated sorted array using binary search. If found, return its index; otherwise, return -1.
Set-4
4a. Write a Java program to delete the nth node from the end of a singly linked list.
4b. Write a Java program to find the ceiling and floor of a given number x in a floating-point array with O(logn).
Set-5
5a. Write a Java program to find the element that occurs only once in a sorted array where every other element appears exactly twice.
5b. Write a Java program to reverse a singly linked list and display both the original and the reversed lists.
Set-6
Question:
Write a Java program to find the indices of two numbers in an array that add up to a given target using a HashMap.
Question:
Write a Java program to perform an iterative search for a given element in a singly linked list.
Question:
Write a Java program to perform a recursive search for a given element in a singly linked list.
Set-7
Question:
Write a Java program to remove duplicates from an array and print the elements in sorted order using a TreeSet.
Question:
Set-8
8a) Remove Consecutive Duplicates in String
Question:
Question:
Write a Java program to sort a stack using only another stack as auxiliary space.
Set-9
Question:
Write a Java program to find the equilibrium index of an array where the sum of elements on the left is equal to the sum on the right.
Question:
Write a Java program to construct a Binary Search Tree (BST) and find its diameter (the length of the longest path between any two nodes).
Set-10
Question:
Write a Java program to create the mirror of a Binary Search Tree and print its inorder traversal before and after mirroring.
Question:
Write a Java program to calculate the minimum number of operations to convert one array to another. An operation is defined as
incrementing or decrementing an element by 1.
Set-11
11a. Total Cost of Connections: Given a set of ropes with different lengths, calculate the total cost to connect all ropes. The cost to connect
two ropes is the sum of their lengths, and the total cost is the sum of all connection costs. (Use a priority queue/heap to solve this
efficiently.)
11b. Remove Half Nodes from a BST: Given a binary search tree, remove all half nodes. A half node is a node with only one child.
Implement this and print the inorder traversal of the tree before and after removing half nodes.
Set-12
12a. 1Print Left Nodes of a BST: Given a binary search tree (BST), print the data of all left child nodes.
12b. 1Product of Array Elements Without Division: Given an array, calculate a new array where each element is the product of all the
elements in the original array except the current element, without using division.
Set-13
13a.Assign Cookies to Children: Given an array representing children's greed factors and another array representing cookie sizes, determine
how many children can be satisfied with the available cookies. Each cookie can satisfy one child if its size is greater than or equal to the
child's greed factor.
13b.Width of a Binary Search Tree at a Given Level: Given a binary search tree, calculate the width of the tree at a specific level. The width
at a level is the number of nodes at that level.
Set-14
14a. Lemonade Stand Simulation: Implement a simulation where customers arrive with different payment amounts, and the lemonade
stand provides lemonade and change. The stand has a limited amount of change and can only serve a customer if it has enough change for
the transaction.1
14b. Height of a Binary Search Tree: Given a binary search tree, calculate the height of the tree. The height of a tree is the number of edges
on the longest path from the root to a leaf.
Set-15
15a. Equal Sum Among 3 Stacks: Given three stacks with positive integers, find the maximum equal sum that can be achieved by removing
elements from the stacks. You can only remove elements from the top of the stacks.
15b. Max Width of a Binary Search Tree: Given a binary search tree, find its maximum width. The width of a binary tree at any level is the
number of nodes at that level, and the maximum width is the highest width among all levels of the tree.
Set-16
Set-17
17a.Count all root-to-leaf paths in a binary tree. [ A. No of routes from input n,m. (0,0) to (n,m). ]
Set-18
18a. Algorithm for finding the maximum subarray sum (kadane algorithm)..
Implement Binary search tree and all the Depth First Traversals(DFS)
Set-19
19a.Reverse characters of a string using two pointers technique.[ 1.reverse only string letters(ab-cd) op-(dc-ba)]
19b. Implement two stacks in one array (Dynamic implementation). [implement two stacks using an array(menu driven program with
push,pop,display,isempty,top for each stack)]
Set-20
20a. Find a triplet in an array such that the sum of two elements equals the third element.
20b. Implement Depth-First Search (DFS) using an adjacency list without recursion.
set-21
Write a program that takes the number of vertices and edges of an undirected graph, followed by the edges themselves. Perform a DFS
traversal starting from node 0 and print the visited nodes.
Modify the program in question 1a to perform DFS traversal starting from any given node (not just node 0).
set-22
Write a program that calculates the number of ways to climb a staircase where at each step, you can climb either 1, 2, or 3 stairs. The
program should take the number of stairs n as input.
Implement DFS traversal in an undirected graph, where the number of vertices and edges is provided, and the edges are entered in the
format u v. The traversal should start from node 0.
set-23
23a. Longest Common Subsequence
Write a program that computes the length of the longest common subsequence (LCS) of two strings. The program should take two strings
as input and output the length of their LCS.
Implement a DFS traversal where the starting node for the traversal is provided by the user as input. The program should take the number
of vertices and edges as input, along with the edges, and start DFS from the specified node.
set-24
Write a program to find all unique triplets in an array that sum to zero. The program should take the number of elements and the elements
of the array as input.
Implement BFS traversal in an undirected graph. The program should take the number of vertices, the number of edges, and the edges as
input, and it should start the BFS traversal from a specified node.
set-25
Write a program to find the minimum number of platforms required to accommodate a set of trains given their arrival and departure
times. The program should take the number of trains, along with their arrival and departure times, as input.
Modify the BFS traversal program to allow the user to specify the starting node for the BFS traversal. The program should take the number
of vertices, the number of edges, and the edges as input, along with the starting node.
set-26
1a. Write a Java program to check if the parentheses in a given string are balanced.
1b. Implement a program that checks if the parentheses in a given string are balanced using a stack.
set-27
1a. Write a Java program to find all quadruplets in an array that sum to a given target.
1b. Implement a program to delete the middle element of a stack using recursion.
set-28
1b. Implement a stack using two queues and provide methods to push, pop, top, and check if the stack is empty.
set-29
1a. Write a Java program to find the number of unique paths in a grid with obstacles, where you can only move right or down.[using (dp)]
set-30
1b. Implement a MinStack that supports push, pop, and retrieving the minimum element efficiently
set-31
1a. Write a Java program to find the missing term in a logarithmic sequence where each term is multiplied by a constant ratio. The
sequence will have one missing term.
Eg:{1,3,5,7,11}
o/p:9
1b. Write a Java program to implement a stack that supports retrieving the minimum element in constant time. Implement the push, pop,
and getMin operations.
Build a stack and find minimum element in a stack with Time Complexity O(1) and space complexity O(1)
set-32
1a. Write a Java program to find the intersection of two arrays in O(log n) time complexity using binary search.
1b. Write a Java program to check if a string is a palindrome using a stack and a queue.
set-33
1a. Write a Java program to find the intersection of two arrays using a HashSet.
1b. Write a Java program to check if a string is a palindrome using a stack and a queue.
set-34
1a. Write a Java program to implement a ternary search algorithm for finding a target element in a sorted array.
1b. Write a Java program to check if a linked list is a palindrome using a stack.
set-35
1a. Write a Java program to return a sorted array of squares of the elements from a given sorted array.
1b. Write a Java program to remove the Nth node from the end of a singly linked list.
Set-36
Set-37
Set-38
Set-39
39a. Given an array of N integers, find the length of the longest subsequence of a given sequence such that all elements of the
subsequence are sorted in strictly decreasing order.
Examples:
Input: arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85]
Output: 3
Output: 3
39b. )Given an array of strings arr[], the task is to sort the array of strings according to the frequency of each string, in ascending order. If
two elements have the same frequency, then they are sorted into lexicographical order
Examples:
Explanation:
Explanation:
As both the strings have the same frequency, the array is sorted in the lexicographical order.
Set-40
Set-41