Skip to main content

Posts

Showing posts with the label Snapdeal

Height of a Tree [EASY/IMPORTANT]

Company: MakeMyTrip, FreeCharge, FactSet, CouponDunia, Cadence, Amazon, VMWare, Teradata, Synopsys, Snapdeal, Monotype The height of a binary tree is the number of edges between the tree's root and its furthest leaf. This means that a tree containing a single node has a height of  . Complete the  getHeight  function provided in your editor so that it returns the height of a binary tree. This function has a parameter,  , which is a pointer to the root node of a binary tree.  Note  -The Height of binary tree with single node is taken as zero. Input Format You do not need to read any input from stdin. Our grader will pass the root node of a binary tree to your  getHeight function. Output Format Your function should return a single integer denoting the height of the binary tree. Sample Input Note:  A  binary search tree  is a binary tree in which the value of each parent node's left child is les...

Parenthesis Checker [EASY]

Company: Yatra.com, Walmart Labs, Snapdeal, Oracle, Amazon Question:  Given an expression string exp, examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp. For example, the program should print 'balanced' for exp = “[()]{}{[()()]()}” and 'not balanced' for exp = “[(])” Input: The first line of input contains an integer T denoting the number of test cases.  Each test case consist of a string of expression, in a separate line. Output: Print 'balanced' without quotes if pair of parenthesis are balanced else print 'not balanced' in a separate line. Constraints: 1 ≤ T ≤ 100 1 ≤ |s| ≤ 100 Example: Input: 3 {([])} () ()[] Output: balanced balanced balanced CODE: import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int m = sc.nextInt(); for(int mi=0;mi<m;mi++){    String s= sc.next();  ...

Next larger element [EASY]

Company: Snapdeal, Samsung, PayU, Informatica, CouponDunia, Amazon  Question:  Given an array A [ ] having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, output -1  Input: The first line of input contains a single integer  T  denoting the number of test cases.Then  T  test cases follow. Each test case consists of two lines. The first line contains an integer N denoting the size of the array. The Second line of each test case contains N space separated positive integers denoting the values/elements in the array A[ ].   Output: For each test case, print in a new line, the next greater element for each array element separated by space in order. Constraints: 1<=T<=200 1<=N<=1000 1<=A[i]<=1000 Example: Input 1 4 1 3 2 4  Output 3 4 4 -1 Explanation In the array, the next larger ...

Check for BST [MEDIUM]

Company: MakeMyTrip, GreyOrange, Boomerang, Amazon, Adobe, Accolite, Walmart-Labs, VMWare, Snapdeal, Qualcomm, OYO Rooms, Microsoft, Wooker. Question:  Given a binary tree, return true if it is  BST , else false. For example, the following tree is not BST, because 11 is in left subtree of 10.         10      /     \    7       39      \       11 Input: The task is to complete the method which takes one argument, root of Binary Tree. The struct Node has a data part which stores the data, pointer to left child and pointer to right child. There are multiple test cases. For each test case, this method will be called individually. Output: The function should return 1 if BST else return 0. Constraints: 1 <=T<= 30 1 <= Number of nodes<= 100 1 <= Data of a node<= 1000 Example: Input: 2 2 1 2 R 1 3 L 4 10 20 L 10 30 R 2...

Minimum element in sorted and rotated array [EASY]

Company: Times-Internet, Snapdeal, Morgan Stanley,Microsoft, Amazon, Adobe Question: A sorted array A[ ] with distinct elements is rotated at some unknown point, the task is to find the minimum element in it. Expected Time Complexity:  O(Log n) Input: The first line of input contains a single integer T denoting the number of test cases. Then   T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array. The second line of each test case contains N space separated integers denoting array elements. Output: Corresponding to each test case, in a new line, print the minimum element in the array. Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 500 1 ≤ A[i] ≤ 1000 Example: Input 1 5 4 5 1 2 3 Output 1 CODE: Naive solution import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Sca...