Skip to main content

Posts

Showing posts with the label MAQ-Software

Insertion Sort [EASY/IMP]

Company: Veritas, MAQ-Software, Juniper, Grofers, Dell, Cisco, Accenture Question: Perform Insertion sort CODE: /* Function to sort an array using insertion sort void insertionSort(int arr[], int n) {   GfG obj = new GfG();    for (int i = 1; i < n; i++)       obj.insert(arr, i); } */ class GfG {   // Function to sort an array using insertion sort   void insert(int arr[],int i)   {        int key=arr[i];        int j=i-1;        while (j>=0 && arr[j] > key)             {                 arr[j+1] = arr[j];                 j = j-1;             }             arr[j+1] = key;   } } EXECUTION TIME:0.4s

Factorial of Big numbers [MEDIUM]

Company: Phillips, Microsoft, MAQ-Software, MakeMyTrip, BrowserStack  Question:  Given an integer, the task is to find factorial of the number.   Input: The first line of input contains an integer T denoting the number of test cases.   The first line of each test case is N,the number whose factorial is to be found   Output: Print the factorial of the number in separate line.   Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 1000   Example: Input 3 5 10 2   Output 120 3628800 2 CODE:  import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; class Solution { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); for(int mi=0;mi<m;mi++){    int n = sc.nextInt();     BigInteger  fact= new BigInteger("1");    for(int i=1;i<n+1;i++){         fact= fact.multiply(BigInteger.valueOf(i...

Implement Queue using Stack [EASY/IMPORTANT]

Company:Flipkart, De-Shaw, Amazon, Adobe, Accolite, Inmobi, Microsoft, MAQ-Software, MakeMyTrip, InfoEdge, Goldman-Sachs, Walmart-Labs, Oracle Question: Implement a Queue using 2 stacks  s1  and  s2  . Input  : The first line of the input contains an integer ' T ' denoting the number of test cases. Then T test cases follow. First line of each test case contains an integer  Q  denoting the number of queries .  A Query  Q  is of 2 Types (i)  1 x   (a query of this type means  pushing  'x'  into the queue) (ii)  2     (a query of this type means to pop element from queue and print the poped element) The second line of each test case contains  Q  queries seperated by space. Output: The output for each test case will  be space separated integers having  -1  if the queue is empty else the element poped out from the queue .  You are...

Two Water Jug problem [MEDIUM]

Company: MAQ-Software, MakeMyTrip Question: You are at the side of a river. You are given a  m  litre jug and a  n  litre jug where  0 < m < n . Both the jugs are initially empty. The jugs don’t have markings to allow measuring smaller quantities. You have to use the jugs to measure d litres of water where d < n. Determine the minimum no of operations to be performed to obtain d litres of water in one of jug. The operations you can perform are: Empty a Jug Fill a Jug Pour water from one jug to the other until one of the jugs is either empty or full. Input: First line consists of T test cases. Only line of every test case consists of 3 spaced integers denoting m , n, and d respectively.  Output: Single line output, print the minimum number of operations. Constraints: 1<=T<=100 1<=N,D<=100 1<=M<=N Example: Input: 2 8 56 46 3 5 4 Output: -1 6 Code: import java.util.*; import java.lang.*; import java.io.*;...

Egg Dropping Puzzle: DP [MEDIUM/VERY IMPORTANT]

Company: MakeMyTrip, Hike, Google, Goldman-Sachs, D-E-Shaw, Amazon, Philips, Oracle, Opera, Nearby, Mynta, MAQ-Software, VMWare, Unisys, Service Now, Samsung Question: The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors. Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: …..An egg that survives a fall can be used again. …..A broken egg must be discarded. …..The effect of a fall is the same for all eggs. …..If an egg breaks when dropped, then it would break if dropped from a higher floor. …..If an egg survives a fall then it would survive a shorter fall. …..It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break. In this problem you have to find minimum trials to solve for n eggs and k floors. For more description...

N Queen Problem [Hard]

Company: Visa, Twitter, MAQ-Software, Amdocs, Amazon, Accolite Question: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, print all distinct solutions to the n-queens puzzle. Each solution contains distinct board configurations of the n-queens’ placement, where the solutions are a permutation of [1,2,3..n] in increasing order, here the number in the  ith  place denotes that the  ith -column queen is placed in the row with that number. For eg below figure represents a chessboard [3 1 4 2]. Input: The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the chessboard. Output: For each test case, output your solutions on one line where each solution is enclosed in square brackets '[', ']' separated by a space . The solutions are permutat...