Categories of Questions

## Array Questions Accolite

**Count pair with Given Sum** In problem “count pair with given sum” we have given an integer array[] and another number say ‘sum’, you have to determine whether any of the two elements in a given array has a sum equal to “sum”. Example Input: arr []={1,3,4,6,7} and sum = 9. Output: “ Elements found ...

**Group Multiple Occurrence of Array Elements Ordered by first Occurrence** You are given a question in which you have given an unsorted array with multiple occurrences of numbers. The task is to group all the multiple occurrences of array elements ordered by first occurrence. Meanwhile, the order should be the same as the number comes. Example Input: [ 2, 3,4,3,1,3,2,4] ...

**Maximum difference between frequency of two elements such that element having greater frequency is also greater** Suppose, you have an integer array. The problem statement asks to find out the maximum difference between the frequency of any two distinct elements of a given array, but the element with the greater frequency should also be greater in value than the other integer. Example Input: arr[] = {2,4,4,4,3,2} ...

**All Unique Triplets that Sum up to a Given Value** We have given an array of integers and a given number called ‘sum’. The problem statement asks to find out the triplet that adds up to the given number ‘sum’. Example Input: arr[] = {3,5,7,5,6,1} sum=16 Output: (3, 7, 6), (5, 5, 6) Explanation: Triplet which equals to the given ...

**Segregate 0s and 1s in an Array** Problem Statement Suppose you have an integer array. The problem “Segregate 0s and 1s in an array” asks to segregate the array in two parts, in 0s and in 1s. The 0’s should be on the left side of the array and 1’s on the right side of the array. ...

**Find Largest d in Array such that a + b + c = d** Problem Statement Suppose you have an array of integers. Input values are all distinct elements. The problem “Find largest d in array such that a + b + c = d” asks to find out the largest element ‘d’ in the set such that a + b + c = ...

**Maximum Consecutive Numbers Present in an Array** Problem Statement Suppose you have an array of integers of size N. The problem “Maximum consecutive numbers present in an array” asks to find out the maximum count of consecutive numbers that could be scattered in an array. Example arr[] = {2, 24, 30, 26, 99, 25} 3 Explanation: The ...

**Find whether an array is subset of another array** The problem “Find whether an array is subset of another array” states that you are given two arrays arra1[] and array2[]. The arrays given are in an unsorted manner. Your task is to find whether the array2[] is a subset of array1[]. Example arr1= [1,4,5,7,8,2] arr2= [1,7,2,4] arr2 [] is ...

**Maximum sum of pairs with specific difference** The problem “Maximum sum of pairs with specific difference” states that you are given an array of integers and an integer K. Then we are asked to find out the maximum sum of independent pairs. We can pair two integers if they have an absolute difference of less than K. ...

**Print all triplets in sorted array that form AP** The problem “Print all triplets in sorted array that form AP” states that we have given a sorted integer array. The task is to find out all the possible triplets that can form an Arithmetic Progression. Example arr[] = {1,3,5,7,8,12,15,16,20,30} (1, 3, 5), (3, 5, 7), (1, 8, 15), (8, ...

**Count number of triplets with product equal to given number** The problem “Count number of triplets with product equal to given number” states that we are given an integer array and a number m. The problem statement asks to find out the total number of triplets of with product equals to m. Example arr[] = {1,5,2,6,10,3} m=30 3 Explanation Triplets ...

**Maximum difference between first and last indexes of an element in array** Suppose, you have an array of integers. The problem “Maximum difference between first and last indexes of an element in array” asks to find out the difference between the first and last index of each number present in an array such that the difference is being maximum of all. Example ...

**Find elements which are present in first array and not in second** The problem “Find elements which are present in first array and not in second” states that you are given two arrays. Arrays consist of all the integers. You have to find out the numbers which will not be present in the second array but present in the first array. Example ...

**Maximum product of an increasing subsequence** Problem Statement The problem “Maximum product of an increasing subsequence” states that you are given an array of integers. Now you need to find out the maximum product you can achieve such that you multiply the elements of an increasing subsequence. The thing to note is that, we are not ...

**Form minimum number from given sequence** The problem “Form minimum number from given sequence” states that you are given some pattern of I’s and D’s only. The meaning of I stands for increasing and for decreasing we are provided with D. The problem statement asks to print the minimum number which satisfies the given pattern. We have ...

**Non-overlapping sum of two sets** Problem Statement The problem “Non-overlapping sum of two sets” states that you are given two arrays as input values as arrA[] and arrB[] of the same size n. Also, both of the arrays have distinct elements individually and some common elements. Your task is to find out the total sum ...

**Products of ranges in an array** Problem Statement The problem “Products of ranges in an array” states that you are given an integer array consisting of numbers range from 1 to n and q number of queries. Each query contains the range. The problem statement asks to find out the product within the given range under ...

**First negative integer in every window of size k** Problem Statement The problem “First negative integer in every window of size k” states that you are given an array containing positive and negative integers, for every window of size k print the first negative integer in that window. If there is no negative integer in any window then output ...

**Segregate even and odd numbers** Problem Statement Suppose you have an integer array. The problem “Segregate even and odd numbers” asks to rearrange the array so that the odd and even numbers can be separated in two segments of the array. The even numbers be shifted into the left side of the array and odd ...

**Product of array except self** Problem Statement “Product of array except self” problem, states that you are given an array a [ ]. Print another array p [ ] of the same size such that value at i’th index of array p is equal to the product of all the elements of the original array ...

**First missing positive** Problem Statement “First missing positive” problem states that you are given an array a[ ] (sorted or unsorted) of size n. Find the first positive number that is missing in this array. Example a[ ] = {1, 3, -1, 8} 2 Explanation: If we sort the array we get {-1, ...

**Program for Bridge and Torch problem** Problem Statement The “Bridge and Torch” problem states that you are given an array of time a person needs to cross the bridge. Since it is time, it comprises positive integers. Along with the time we are given a bridge, which a person needs to cross. The bridge allows only ...

**Count quadruples from four sorted arrays whose sum is equal to a given value x** Problem Statement Problem “Count quadruples from four sorted arrays whose sum is equal to a given value x” state that you are given four integer arrays and a value called x. The problem statement asks to find out how many quadruplets can be formed of which sum of elements of ...

**Numbers with prime frequencies greater than or equal to k** Problem Statement Problem “Numbers with prime frequencies greater than or equal to k” states that you are given an array of integers size n and an integer value k. All the numbers inside it are prime numbers. The problem statement asks to find out the numbers which appear in the ...

**Maximum Subarray Sum Excluding Certain Elements** Problem Statement We are given an array, and we need to find maximum subarray sum excluding certain elements. That is, we need to find the max sum of subarray such that the subarray we are considering does not contain the elements which are told to be excluded. Example of maximum ...

**Find minimum number of merge operations to make an array palindrome** Problem Statement You are given an array of integers. The problem statement asks to find minimum number of merge operations to make an array palindrome, i.e. find out the minimum number of merging operations to be done on the array to make it a palindrome. Merging operation simply means that ...

**Maximum sum rectangle in a 2D matrix** Problem Statement Find the maximum sum rectangle in a 2D matrix i.e. to find a sub-matrix with maximum sum. A sub-matrix is nothing but a 2D array inside of the given 2D array. So, you have a matrix of signed integers, you need to calculate the sum of sub-matrices and ...

**Largest Sum Contiguous Subarray** Problem Statement You are given an array of integers. The problem statement asks to find out the largest sum contiguous subarray. This means nothing but to find a subarray (continuous elements) which has the largest sum among all other subarrays in the given array. Example arr[] = {1, -3, 4, ...

**Count Distinct Elements in Every Window of Size K** Subsets are something which we have been dealing with for some time now. In the last episode, we covered the number of subsets we could make with distinct even numbers. This time we count distinct elements in every window of size K. Section-1 About the problem. Given an unsorted array ...

**Count Pairs Whose Products Exist in Array** In count pairs whose products exist in array problem we have given an array, count all the distinct pairs whose product value is present in the array. Example Input A[]={2, 5, 6, 3, 15} Output Number of distinct pairs whose product exists in the array is: 2 Pairs are: (2, ...

**Count Pairs With Given Sum** Given an integer array of size n, and an integer ‘K’, you need to count the number of pairs(need not to be unique) present in the array whose sum is equal to ‘K’. Example Input: Arr={1, 5, 7, 1} K=6 Output: 2 Brute force solution for Count Pairs With Given Sum Main idea ...

**Check if an Array is Stack Sortable** In check if an array is stack sortable problem we have given an array a[ ] of size n containing elements from 1 to n in random order. Sort the array in ascending order using a temporary stack following only these two operations – Remove the element at the starting ...

**Find Top K (or Most Frequent) Numbers in a Stream** In find top k (or most frequent) numbers in a stream problem, we have given an integer array consisting of some numbers. The problem statement says that you have to take an element from the array, and you can only have at most k numbers at the top. We need ...

**Number of NGEs to the Right** In the Number of NGEs to the right problem we have given an array a[ ] of size n and q number of queries representing the index of the array. For each query, i print the total number of next greater elements to it’s right. Example Input a[ ] = ...

**Find the Subarray of given length with Least Average** Problem Statement In the “Find the Subarray of given length with Least Average” problem we have given an array and an input integer X. Write a program to find the subarray of length X with least/minimum average. Prints the starting and ending indexes of the subarray which has the least ...

**Find Zeros to be Flipped so that Number of Consecutive 1’s is Maximized** Problem Statement In the “Find Zeros to be Flipped so that Number of Consecutive 1’s is Maximized” problem we have given a binary array and a number x which denotes the no. of zeros to be flipped. Write a program to find the zeros that need to be flipped so ...

**Find the two Numbers with Odd Occurrences in an Unsorted Array** Problem Statement In the “Find the two Numbers with Odd Occurrences in an Unsorted Array” problem we have given an unsorted array. In this array other than two numbers all other numbers occur even number of times. Find the two numbers that occur an odd number of times. Note: The ...

**Implement Two Stacks in an Array** Problem Statement In the “Implement Two Stacks in an Array” problem we have to implement two stacks in an array such that, if the user wants to push an element in either of two stacks then there should not be an error till the array gets full. Example Push 5 ...

**Tug of War** Problem Statement In tug of war problem, we have given an array of integers, divide the array into two subsets of size n/2 size each so that the difference of the sum of two subsets is as minimum as possible. If n is even each subset size is n/2. If ...

**Partition Problem** Problem Statement In the Partition problem, we have given a set that contains n elements. Find whether the given set can be divided into two sets whose sum of elements in the subsets is equal. Example Input arr[] = {4, 5, 11, 9, 8, 3} Output Yes Explanation The array ...

**Find the Lost Element From a Duplicated Array** Problem Statement Given two arrays A and B, one array is a duplicate of the other except one element. The one element is missing from either A or B. we need to find the lost element from a duplicated array. Example 5 1 6 4 8 9 6 4 8 ...

**Find Triplet in Array With a Given Sum** Problem Statement Given an array of integers, find the combination of three elements in the array whose sum is equal to a given value X. Here we will print the first combination that we get. If there is no such combination then print -1. Example Input N=5, X=15 arr[] = ...

**Smallest Positive Number Missing in an Unsorted Array** Problem Statement In the given unsorted array find the smallest positive number missing in an unsorted array. A positive integer doesn’t include 0. We can modify the original array if needed. The array may contain positive and negative numbers. Example a. Input array : [3, 4, -1, 0, -2, 2, 1, ...

**Maximum Sum of Non Consecutive Elements** Problem Statement In the “Maximum Sum of Non Consecutive Elements” given array, you need to find the maximum sum of non-consecutive elements. You can not add immediate neighbor numbers. For example [1,3,5,6,7,8,] here 1, 3 are adjacent so we can’t add them, and 6, 8 are not adjacent so we ...

**Multiplication of Previous and Next** Problem Statement Multiplication of Previous and Next: In the given array replace every element with the product of next and previous elements to it. And for the first element(a[0]) we need to replace it with the product of next and itself, for the last element(a[n-1]) we need to replace it ...

**A Product Array Puzzle** Problem Statement In a product array puzzle problem we need to construct an array where the ith element will be the product of all the elements in the given array except element at the ith position. Example Input 5 10 3 5 6 2 Output 180 600 360 300 900 ...

## String Questions Accolite

**Form minimum number from given sequence** The problem “Form minimum number from given sequence” states that you are given some pattern of I’s and D’s only. The meaning of I stands for increasing and for decreasing we are provided with D. The problem statement asks to print the minimum number which satisfies the given pattern. We have ...

**Rearrange a binary string as alternate x and y occurrences** Problem Statement Suppose you are given a binary string, and two numbers x and y. The string consists of 0s and 1s only. The problem “Rearrange a binary string as alternate x and y occurrences” asks to rearrange the string such that the 0 comes x times ⇒ 1 comes ...

**Reverse words in a string** Problem Statement “Reverse words in a string” states that you are given a string s of size n. Print the string in reverse order such that the last word becomes the first, second last becomes the second, and so on. Hereby string we refer to a sentence containing words instead ...

**KMP Algorithm** KMP(Knuth-Morris-Pratt) algorithm is used for pattern searching in a given string. We are given a string S and a pattern p, our goal is to determine whether or not the given pattern is present in the string. Example Input: S = “aaaab” p = “aab” Output: true Naive Approach The ...

**Reverse a String using Stack** We have given a string s of length n which contains lower case letters, upper case letters, integers, and some special symbol. Reverse the given string using stack. Let’s see some examples for better understanding. Example Input s = “TutorialCup” Output puClairotuT Input s = “Stack” Output kcatS Using Stack ...

**Rabin Karp Algorithm** Rabin Karp Algorithm used to find the pattern string in the given text string. There are so many types of algorithms or methods used to find the pattern string. In this algorithm, we use Hashing for finding the pattern matching. If we got the same hash code for the substring ...

**Sort a String According to Another String** Problem Statement Given two input strings, a pattern and a string. We need to sort the string according to the order defined by the pattern. Pattern string has no duplicates and it has all characters of the string. Input Format The first line containing a string s which we need ...

**Longest Common Prefix using Divide and Conquer** Problem Statement In the “Longest Common Prefix using Divide and Conquer ” problem, we have given an integer n and n strings. Write a program that will print the longest common prefix. If there is no common prefix then print “-1”. Input Format The first line contains an integer n. ...

**Print Shortest Path to Print a String on Screen** Problem Statement In the “Print Shortest Path to Print a String on Screen” problem we have given a screen containing alphabets from A-Z and input string, by using remote we can go from one character to another character, remote contains only left, right, top, and bottom keys. write a function ...

**Online Algorithm for Checking Palindrome in a Stream** Problem Statement In the “Online Algorithm for Checking Palindrome in a Stream” problem, we have given a stream of characters(charcaters are received one by one). Write a program that will print ‘yes’ every time if the received characters till now form a palindrome. Input Format The first and only one ...

**Check if Two given Strings are Isomorphic to each other** Problem Statement In the “Check if Two given Strings are Isomorphic to each other” problem we have given two strings s1 and s2. Write a program that says whether the given strings are isomorphic or not. Note: Two strings are said to be isomorphic if there is a one to ...

## Tree Questions Accolite

**Given a binary tree, how do you remove all the half nodes?** The problem “Given a binary tree, how do you remove all the half nodes?” states that you are given a binary tree. Now you need to remove the half nodes. A half node is defined as a node in the tree that has only a single child. Either it is ...

**Boundary Traversal of binary tree** Problem Statement The problem “Boundary Traversal of binary tree” states that you are given a binary tree. Now you need to print the boundary view of a binary tree. Here boundary traversal means that all the nodes are shown as the boundary of the tree. The nodes are seen from ...

**Bottom View of a Binary Tree** Problem Statement The problem “Bottom View of a Binary Tree” states that you are given a binary tree and now you need to find the bottom view for the given tree. When we see a tree from the downward direction. The nodes which are visible to us is the bottom ...

**Print Right View of a Binary Tree** Problem Statement The problem “Print Right View of a Binary Tree” states that you are given a binary tree. Now you need to find the right view of this tree. Here, right view of the binary tree means to print the sequence as the tree looks when looked from the ...

**Binary Search Tree Delete Operation** Problem Statement The problem “Binary Search Tree Delete Operation” asks us to implement the delete operation for binary search tree. Delete function refers to the functionality to delete a node with a given key/data. Example Input Node to be deleted = 5 Output Approach for Binary Search Tree Delete Operation So ...

**Iterative Method to find Height of Binary Tree** Problem Statement The problem “Iterative Method to find Height of Binary Tree” states that you are given a binary tree, find the height of the tree using the iterative method. Examples Input 3 Input 4 Algorithm for Iterative Method to find Height of Binary Tree The height of a tree ...

**Clone a Binary Tree with Random Pointers** Problem Statement You are given a complete binary tree with some random pointers. Random pointers are referred to nodes which every node points to other than its left and right child. So, this also changes the standard structure of a node in a simple binary tree. Now the node of ...

**Find k-th smallest element in BST (Order Statistics in BST)** Problem Statement “Find k-th smallest element in BST (Order Statistics in BST)” problem states that you are given a binary search tree and you need to find the k-th smallest number in the BST. This means if we do an in-order traversal of the binary search tree and store the ...

**A program to check if a binary tree is BST or not** Problem Statement “A program to check if a binary tree is BST or not” states that you are given a binary tree and you need to check if the binary tree satisfies the properties of the binary search tree. So, the binary tree has the following properties: The left subtree ...

**Print Ancestors of a Given Binary Tree Node Without Recursion** Given a binary tree and a specific node or key. Print ancestors of a given binary tree node without recursion. Example Input : key = 7 Output : 3 1 Input : key = 4 Output : 2 1 Algorithm for Ancestors of a Given Binary Tree Node Create a class Node ...

**Print a Binary Tree in Vertical Order** In this problem, we have given a pointer denoting the root of the binary tree and your task is to print the binary tree in the vertical order. Example Input 1 / \ 2 3 / \ / \ 4 5 6 7 \ \ 8 9 Output 4 2 ...

## Graph Questions Accolite

**Topological Sorting** Given a directed acyclic graph, topologically sort the graph nodes. Topological Sorting Example Topological sorting of above graph is -> {1,2,3,0,5,4} Theory Topological Sorting is done for a Directed Acyclic Graph (DAG). A DAG has no cycles in it. ie, there is no such path starting from any node of ...

**Dijkstra Algorithm** Dijkstra is the shortest path algorithm. Dijkstra algorithm is used to find the shortest distance of all nodes from the given start node. It logically creates the shortest path tree from a single source node, by keep adding the nodes greedily such that at every point each node in the ...

## Stack Questions Accolite

**Form minimum number from given sequence** The problem “Form minimum number from given sequence” states that you are given some pattern of I’s and D’s only. The meaning of I stands for increasing and for decreasing we are provided with D. The problem statement asks to print the minimum number which satisfies the given pattern. We have ...

**Print Ancestors of a Given Binary Tree Node Without Recursion** Given a binary tree and a specific node or key. Print ancestors of a given binary tree node without recursion. Example Input : key = 7 Output : 3 1 Input : key = 4 Output : 2 1 Algorithm for Ancestors of a Given Binary Tree Node Create a class Node ...

**Queue using Stacks** In queue using a stack problem, we have to implement the following functions of a queue using the standard functions of stack data structure, Enqueue: Add an element to the end of the queue Dequeue: Remove an element from the start of the queue Example Input: Enqueue(5) Enqueue(11) Enqueue(39) Dequeue() ...

**Reversing a Queue** In Reversing a Queue problem we have given a queue, write an algorithm to reverse the queue. Examples Input queue = 10 -> 8 -> 4 -> 23 Output queue = 23->4->8->10 Input queue = 11 -> 98 -> 31 -> 42 -> 73 -> 6 Output queue = 6 ...

**Check if an Array is Stack Sortable** In check if an array is stack sortable problem we have given an array a[ ] of size n containing elements from 1 to n in random order. Sort the array in ascending order using a temporary stack following only these two operations – Remove the element at the starting ...

**Reverse a String using Stack** We have given a string s of length n which contains lower case letters, upper case letters, integers, and some special symbol. Reverse the given string using stack. Let’s see some examples for better understanding. Example Input s = “TutorialCup” Output puClairotuT Input s = “Stack” Output kcatS Using Stack ...

**Number of NGEs to the Right** In the Number of NGEs to the right problem we have given an array a[ ] of size n and q number of queries representing the index of the array. For each query, i print the total number of next greater elements to it’s right. Example Input a[ ] = ...

**Implement Two Stacks in an Array** Problem Statement In the “Implement Two Stacks in an Array” problem we have to implement two stacks in an array such that, if the user wants to push an element in either of two stacks then there should not be an error till the array gets full. Example Push 5 ...

## Queue Questions Accolite

**Iterative Method to find Height of Binary Tree** Problem Statement The problem “Iterative Method to find Height of Binary Tree” states that you are given a binary tree, find the height of the tree using the iterative method. Examples Input 3 Input 4 Algorithm for Iterative Method to find Height of Binary Tree The height of a tree ...

**First negative integer in every window of size k** Problem Statement The problem “First negative integer in every window of size k” states that you are given an array containing positive and negative integers, for every window of size k print the first negative integer in that window. If there is no negative integer in any window then output ...

**Queue using Stacks** In queue using a stack problem, we have to implement the following functions of a queue using the standard functions of stack data structure, Enqueue: Add an element to the end of the queue Dequeue: Remove an element from the start of the queue Example Input: Enqueue(5) Enqueue(11) Enqueue(39) Dequeue() ...

**Reversing a Queue** In Reversing a Queue problem we have given a queue, write an algorithm to reverse the queue. Examples Input queue = 10 -> 8 -> 4 -> 23 Output queue = 23->4->8->10 Input queue = 11 -> 98 -> 31 -> 42 -> 73 -> 6 Output queue = 6 ...

## Matrix Questions Accolite

**Maximum sum rectangle in a 2D matrix** Problem Statement Find the maximum sum rectangle in a 2D matrix i.e. to find a sub-matrix with maximum sum. A sub-matrix is nothing but a 2D array inside of the given 2D array. So, you have a matrix of signed integers, you need to calculate the sum of sub-matrices and ...

**Print Shortest Path to Print a String on Screen** Problem Statement In the “Print Shortest Path to Print a String on Screen” problem we have given a screen containing alphabets from A-Z and input string, by using remote we can go from one character to another character, remote contains only left, right, top, and bottom keys. write a function ...

## Other Questions Accolite

**Union and Intersection of two Linked Lists** Given two linked lists, create another two linked lists to get union and intersection of the elements of existing lists. Example Input: List1: 5 → 9 → 10 → 12 → 14 List2: 3 → 5 → 9 → 14 → 21 Output: Intersection_list: 14 → 9 → 5 Union_list: ...

**Total Numbers With no Repeated Digits in a Range** You are given a range of numbers (start, end). The given task says to find out the total numbers of numbers with no repeated digits in a range. Example Input: 10 50 Output: 37 Explanation: 10 has no repeated digit. 11 has a repeated digit. 12 has no repeated digit. ...

**Write a function to get the intersection point of two Linked Lists** Problem Statement The problem “Write a function to get the intersection point of two Linked Lists” states that you are given two linked lists. But they are not independent linked lists. They are connected at some point. Now you need to find this point of intersection of these two lists. ...

**Linked List Cycle** Problem Statement “Linked List Cycle” problem states that you are given a linked list. Find if it contains any loop or not? Linked list with cycle Example 1->2->3 No Loop Explanation: The linked list does not contain any loop because if it did then there would’ve been two no des ...

**Find Number of Employees Under every Employee** HashMaps are one of the most useful data structures. Find the number of employees under every employee is a problem that reminds me of the famous movie’s inception. Akin to dream in a dream. Here, we have an employee working under an employee and so on. Problem Statement So, what ...

**Top K Frequent Words** In top K frequent words problem, we have given a list of words and an integer k. Print k most frequently used strings in the list. Example Input : list = {“code”, “sky”, “pen”, “sky”, “sky”, “blue”, “code”} k = 2 Output : sky code Input : list = {“yes”, ...

**N queen problem** N queen problem using the concept of Backtracking. Here we place queen such that no queen under attack condition. The attack condition of the queens is if two queens are on the same column, row, and diagonal then they are under attack. Let’s see this by the below figure. Here ...

**Reverse a linked list** Problem Statement The problem “reverse a linked list” states that we are given the head of the linked list. We have to reverse the linked list by changing the links between them and return the head of the reversed linked list. Example 10->20->30->40->NULL NULL<-10<-20<-30<-40 Explanation We have reversed the linked ...

**Find Nth Node** Problem Statement In the “Find Nth Node” problem we have given a linked list to find the nth node. The program should print the data value in the nth node. N is the input integer index. Example 3 1 2 3 4 5 6 3 Approach Given a linked list ...