Posts

Showing posts from September, 2020

Reverse LL (Iterative)

Given a linked list, reverse it iteratively. You don't need to print the elements, just reverse the LL duplicates and return the head of updated LL. Input format : Linked list elements (separated by space and terminated by -1) ` Sample Input 1 : 1 2 3 4 5 -1 Sample Output 1 : 5 4 3 2 1

Length of LL (recursive)

  Given a linked list, find and return the length of input LL recursively. Input format : Linked list elements (separated by space and terminated by -1) Output format : Length of LL Sample Input : 3 4 5 2 6 1 9 -1 Sample Output : 7

Code : Merge Sort

Given a singly linked list of integers, sort it using 'Merge Sort.' Note : No need to print the list, it has already been taken care. Only return the new head to the list. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first and the only line of each test case or query contains the elements of the singly linked list separated by a single space. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element Output format : For each test case/query, print the elements of the sorted singly linked list. Output for every test case will be printed in a seperate line. Constraints : 1 <= t <= 10^2 0 <= M <= 10^5 Where M is the size of the singly linked list. Time Limit: 1sec Sample Input 1 : 1 10 9 8 7 6 5 4 3 -1 Sample Output 1 : 3 4 5 6 7 8 9 10  Sample Output 2 : 2 -1 10

Code : Merge two sorted LL

You have been given two sorted(in ascending order) singly-linked lists of integers. Write a function to merge them in such a way that the resulting singly linked list is also sorted(in ascending order) and return the new head to the list. Note : Try solving this in O(1) auxiliary space. No need to print the list, it has already been taken care. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the first sorted singly linked list separated by a single space. The second line of the input contains the elements of the second sorted singly linked list separated by a single space. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element Output : For each test case/query, print the resulting sorted singly linked list, separated

Midpoint of Linked list

  For a given singly linked list of integers, find and return the node present at the middle of the list. Note : If the length of the singly linked list is even, then return the first middle node. Example: Consider, 10 -> 20 -> 30 -> 40 is the given list, then the nodes present at the middle with respective data values are, 20 and 30. We return the first node with data 20.  Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first and the only line of each test case or query contains the elements of the singly linked list separated by a single space. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element  Output Format : For each test case/query, print the data value of the node at the middle of the given list. Output for every test case will be printed in a seperate line. C

Array Insert

Gary likes to solve problems of array, but he doesn't like problems that involve queries. His school teacher gave him an assignment full of problems but one of them involve queries. Can you help Gary in solving that problem so that he can go and play with his friends? The problem is : Given an Array A consisting of N positive integers, you have to answer Q queries on it. Queries can be of the two types: * 1 X Y - Update X at location Y in the array. * 2 L R - Print the sum of range [L, R], i.e. Both L and R are inclusive. Note :- Array indexing is from 0. Input : The first line contains two space separated integers N ( Length of Array ) and Q ( Queries ). Next Line contains N space separated integers denoting array A . Next Q Line contains 3 space separated integers for each line , as described above Output : Answer to the each query of type 2 in a new line, only when range is valid, otherwise ouput `-1` Constraints:   1 <= N <= 10^9 1 <= Q &l

Construct Tree Using Inorder and PostOrder

Given Postorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals. You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Post order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line (separated by space). Sample Input : 8 8 4 5 2 6 7 3 1 4 8 2 5 1 6 3 7 Sample Output : 1 2 3 4 5 6 7 8

Construct Tree Using Inorder and Preorder

Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals. You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line (separated by space). Sample Input : 12 1 2 3 4 15 5 6 7 8 10 9 12 4 15 3 2 5 1 6 10 8 7 9 12 Sample Output : 1 2 6 3 5 7 4 8 9 15 10 12

Print Levelwise

Given a binary tree, print the tree in level wise order. For printing a node with data N, you need to follow the exact format - N:L:x,R:y wherer, N is data of any node present in the binary tree. x and y are the values of left and right child of node N. Print -1. if any child is null. There is no space in between. You need to print all nodes in the level order form in different lines. Input format : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Sample Input : 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1 Sample Output : 8:L:3,R:10 3:L:1,R:6 10:L:-1,R:14 1:L:-1,R:-1 6:L:4,R:7 14:L:13,R:-1 4:L:-1,R:-1 7:L:-1,R:-1 13:L:-1,R:-1

Diameter Of Binary Tree

Given a Binary Tree, find and return the diameter of input binary tree. Diameter is - largest count of nodes between any two leaf nodes in the binary tree (both the leaf nodes are inclusive). Input format : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Output Format : diameter Sample Input : 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1 Sample Output : 7

Mirror Binary Tree

Image
Mirror the given binary tree. That is, right child of every nodes should become left and left should become right. Note : You don't need to print or return the tree, just mirror it. Input format : Line 1 : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Output format : Elements in level order form (Every level in new line) Sample Input 1: 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1 Sample Output 1: 1 3 2 7 6 5 4 Sample Input 2: 5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -1 Sample Output 2: 5 6 10 3 2 9

Nodes without sibling

Given a binary tree, print all nodes that don’t have a sibling. Edit : Print the elements in different lines. And order of elements doesn't matter. Input format : Elements in level order form (separated by space). If any node does not have left or right child, take -1 in its place. Output format : Print nodes separated by new line. Sample Input : 5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1 Sample Output : 9

Is node present?

Given a binary tree and an integer x, check if node with data x is present in the input binary tree or not. Return true or false. Input format : Line 1 : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Line 2 : Integer x Output Format : true or false Sample Input : 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1 7 Sample Output : true

Replace Node With Depth

Image
Given a a binary tree. Replace each of it's data with the depth of tree. Root is at depth 0, change its value to 0 and next level nodes are at depth 1 and so on. Print the tree after modifying in inorder fashion. Example: Input Output Input format : Line 1 : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Output Format : Inorder traversal of modified tree. Sample Input 1: 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1 Sample Output 1: 2 1 2 0 2 1 2

Height Of Tree

Given a binary tree, find and return the height of given tree. Input format : Nodes in the level order form (separated by space). If any node does not have left or right child, take -1 in its place Output format : Height Constraints : 1 <= N <= 10^5 Sample Input : 10 9 4 -1 -1 5 8 -1 6 -1 -1 3 -1 -1 -1 Sample Output : 5

Nodes Greater Than X

Given a Binary Tree and an integer x, find and return the count of nodes which are having data greater than x. Input format : Line 1 : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Line 2 : Integer x Output Format : count Sample Input : 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1 8 Sample Output : 3

Postorder Binary Tree

Given a binary tree, print the postorder traversal of a given tree. Post-order traversal is: LeftChild RightChild Root Input format : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Output Format : Post-order traversal, elements separated by space Sample Input : 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1 Sample Output : 1 4 7 6 3 13 14 10 8

Preorder Binary Tree

Given a binary tree, print the preorder traversal of given tree. Pre-order traversal is: Root LeftChild RightChild Input format : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place) Output Format : Pre-order traversal, elements separated by space Sample Input : 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1 Sample Output : 8 3 1 6 4 7 10 14 13

Sum Of Nodes

Given a binary tree, find and return the sum of all nodes. Input format : Elements in level order form (separated by space). If any node does not have left or right child, take -1 in its place. Sample Input : 5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1 Sample Output : 35

Queue

 

Queue Using LL

  You need to implement a Queue class using linked list. All the required data members should be private. Implement the following public functions : 1. Constructor - Initialises the data members. 2. enqueue : This function should take one argument of type T and has return type void. This function should insert an element in the queue. Time complexity should be O(1). 3. dequeue : This function takes no input arguments and has return type T. This should removes the first element which is entered and return that element as an answer. Time complexity should be O(1). 4. front : This function takes no input arguments and has return type T. This should return the first element which is entered and return that element as an answer. Time complexity should be O(1). 5. size : Return the size of stack i.e. count of elements which are present ins stack right now. Time complexity should be O(1). 6. isEmpty : Checks if the queue is empty or not. Return true or false.

Spot the Bug Challenge 1

 Level EASY A solution to the problem is given to you. This code contains some errors. You have to remove those errors and submit the correct code. Problem Description: Given two integral values, N and p. Your task is to print N rows, each row has to print integers from 1 to p, skipping integers 3 and p. Input format: The first and only line of input contains two space separated integers. Constraints: Time Limit: 1 second. Value of N and p lies in the range: [1, 1000] Output format: N rows are getting printed up to the largest integral value till p (p not included). Sample Input1: 5 7 Sample Output1: 1 2 4 5 6 1 2 4 5 6 1 2 4 5 6 1 2 4 5 6 1 2 4 5 6 Sample Input1: 5 8 Sample Output1: 1 2 4 5 6 7 1 2 4 5 6 7 1 2 4 5 6 7 1 2 4 5 6 7 1 2 4 5 6 7

Fitting circles

You are given a rectangle of length   a   and width   b . You are required to determine a circle that contains the maximum circumference that fits inside the rectangle. This type of circle is called a big circle. Your task is to determine the maximum number of big circles that can fit inside the rectangle.  Input format First line: An integer   t  denoting the number of test cases First line of each test case: Integers   a   and   b Output format For each test case, print the answer on a new line denoting the maximum number of big circles that can fit in the provided rectangle.   Constraints 1 ≤ t ≤ 1000 1 ≤ a , b ≤ 10 9 SAMPLE INPUT   1 40 10 SAMPLE OUTPUT   4 Explanation 4 circles of radius 10 can fit inside the rectangle.

Popular posts from this blog

How to pass parameters in webhook?

Access and modify all the resources of our Wiki.js using WikiJS API

Fahrenheit to Celsius