Suppose we have some sticks with positive integer lengths. We can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. This will be performed until there is one stick remaining. We have to find the minimum cost of connecting all the given sticks into one stick in this way. So if the stack is [2, 4, 3], then the output will be 14.To solve this, we will follow these steps −Define a max heap priority queue pqinsert all elements of s into pqans := 0while pq has more than ... Read More
Suppose we have to design a file system which provides these two functions −createPath(path, value) − This creates a new path and associates a value to it if possible and returns True. It returns False if the path already exists or its parent path doesn't exist.get(path) − This finds the value associated with a path or returns -1 if the path doesn't exist.The format of a path is one or more concatenated strings of the form − (forward slash) / followed by one or more lowercase English letters. For example, /programming and /programming/problems are valid paths while an empty string ... Read More
Suppose we have the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. We have to return the smallest level X such that the sum of all the values of nodes at level X is maximal. So if the tree is like −Then the output will be 2, The level 1 sum = 1, level 2 sum is 7 + 0 = 7, level 2 sum is 7 + (-8) = -1, so max is of level 2, so output is 2.To solve this, we will follow ... Read More
Suppose we have two traversal sequences Preorder and Postorder, we have to generate the binary tree from these two sequences. So if the sequences are [1, 2, 4, 5, 3, 6, 7], [4, 5, 2, 6, 7, 3, 1], then the output will beTo solve this, we will follow these steps −ans := make a tree node by taking value pre[0], stack := empty stack, and insert ansi := 1 and j := 0while i < length of pre and j < length of postif stack top value = post[j], then increase j by 1, pop from stack, and go ... Read More
Suppose one encoded string S is given. We have to find and write the decoded string to a tape, here the encoded string is read one character at a time and the following steps are performed −If the character read is a letter, that letter is simply written onto the tape.If the character read is a digit, the entire current tape is repeatedly written digit – 1 more times in total.Now if some encoded string S, and an index K is given, find and return the K-th letter (starting indices from 1) in the decoded string.So if the string is ... Read More
Suppose we have d dice, and each die has f number of faces numbered 1, 2, ..., f. We have to find the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of the face up numbers equal to the target. So if the input is like d = 2, f = 6, target = 7, then the output will be 6. So if we throw each dice with 6 faces, then there are 6 ways to get sum 6, as 1 + 6, 2 + 5, 3 + ... Read More
Suppose we have jobs difficulty[i] and this array indicates the difficulty of the ith job, and profit[i] is the profit of the ith job. Now consider we have some workers. worker[i] is the ability of the ith worker, this means that this worker can only complete a job with difficulty at most worker[i]. Every worker can do at most one job, but one job can be completed multiple times. We have to find what is the most profit we can make?For example, if the input is like difficulty = [2, 4, 6, 8, 10] and profit = [10, 20, 30, ... Read More
Suppose we have a binary array data, we have to find the minimum number of swaps required to group all 1’s stored in the array together in any place in the array. So if the array is like [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1], then the output will be 3, as possible solution is [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]To solve this, we will follow these steps −set one := 0, n:= length of data arraymake an array summ of size n, and fill this with 0, set summ[0] := ... Read More
Suppose we have a string S and a dictionary of words words, find the number of words[i] that is a subsequence of S. So if the input is S= “abcde” and dictionary is [“a”, “bb”, “acd”, “ace”], then output will be 3. Because there are three sequence of words in the dictionary, that are a subsequence of S: “a” “acd” and “ace”To solve this, we will follow these steps −n := size of words arraycreate one map mfor i in range 0 to size of wordsinsert words[i] into the map m[words[i, 0]] positionans := 0for i in range 0 to ... Read More
Suppose we have a string S, check whether the letters can be rearranged so that two characters that are adjacent to each other are not the same. If that is possible, output any possible result. If that is not possible, return the empty string. So if the input is like “AAB”, then the output will be “ABA”.To solve this, we will follow these steps −Make a priority queue of integer character pairs called pq, define one map mn := size of the stringstore the character frequency in map mfor each key-value pair p in minsert (integer part of p, character ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP