Posts

Showing posts from July, 2022

Minimum Bracket Reversal

For a given expression in the form of a string, find the minimum number of brackets that can be reversed in order to make the expression balanced. The expression will only contain curly brackets. If the expression can't be balanced, return -1. Example : Expression : {{{{ If we reverse the second and the fourth opening brackets, the whole expression will get balanced. Since we have to reverse two brackets to make the expression balanced, the expected output will be 2. Expression : {{{ In this example, even if we reverse the last opening bracket, we would be left with the first opening bracket and hence will not be able to make the expression balanced and the output will be -1. Input Format : The first and the only line of input contains a string expression, without any spaces in between.   Output Format : The only line of output will print the number of reversals required to balance the whole expression. Prints -1, otherwise. Note :You don't have to print anything. It has alrea

Stock Span

Afzal has been working with an organization called 'Money Traders' for the past few years. The organization is into the money trading business. His manager assigned him a task. For a given array/list of stock's prices for N days, find the stock's span for each day. The span of the stock's price today is defined as the maximum number of consecutive days(starting from today and going backwards) for which the price of the stock was less than today's price. For example, if the price of a stock over a period of 7 days are [100, 80, 60, 70, 60, 75, 85], then the stock spans will be [1, 1, 1, 2, 1, 4, 6]. Explanation: On the sixth day when the price of the stock was 75, the span came out to be 4, because the last 4 prices(including the current price of 75) were less than the current or the sixth day's price. Similarly, we can deduce the remaining results. Afzal has to return an array/list of spans corresponding to each day's stock's price. Help him to achi

Longest Increasing Subsequence

Given an array with N elements, you need to find the length of the longest subsequence in the given array such that all elements of the subsequence are sorted in strictly increasing order. Input Format The first line of input contains an integer N. The following line contains N space separated integers, that denote the value of elements of array. Output Format The first and only line of output contains the length of longest subsequence. Constraints 1 <= N <= 10^3 Time Limit: 1 second Sample Input 1 : 6 5 4 11 1 16 8 Sample Output 1 : 3 Sample Output Explanation Length of longest subsequence is 3 i.e. (5,11,16) or (4,11,16). Sample Input 2 : 3 1 2 2 Sample Output 2 : 2  

Delete node (recursive)

Given a singly linked list of integers and position 'i', delete the node present at the 'i-th' position in the linked list recursively.  Note : Assume that the Indexing for the linked list always starts from 0. 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 line of each test case or query contains the elements of the singly linked list separated by a single space. The second line of input contains a single integer depicting the value of 'i'. 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 updated singly linked list. Output for every test case will be printed in a seperate line. C

Find path in BST

Given a BST and an integer k. Find and return the path from the node with data k and root (if a node with data k is present in given BST) in a list. Return empty list otherwise. Note: Assume that BST contains all unique elements. Input Format : The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line of input contains an integer, that denotes the value of k. Output Format : The first line and only line of output prints the data of the nodes in the path from node k to root. The data of the nodes is separated by single space. Constraints: Time Limit: 1 second Sample Input 1: 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 2 Sample Output 1: 2 5 8  

Construct BST

Given a sorted integer array A of size n, which contains all unique elements. You need to construct a balanced BST from this input array. Return the root of constructed BST. Note: If array size is even, take first mid as root. Input format: The first line of input contains an integer, which denotes the value of n. The following line contains n space separated integers, that denote the values of array. Output Format: The first and only line of output contains values of BST nodes, printed in pre order traversal. Constraints: Time Limit: 1 second Sample Input 1: 7 1 2 3 4 5 6 7 Sample Output 1: 4 2 1 3 6 5 7  

Elements Between K1 and K2

Given a Binary Search Tree and two integers k1 and k2, find and print the elements which are in range k1 and k2 (both inclusive). Print the elements in increasing order. Input format: The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line contains two integers, that denote the value of k1 and k2. Output Format: The first and only line of output prints the elements which are in range k1 and k2 (both inclusive). Print the elements in increasing order. Constraints: Time Limit: 1 second Sample Input 1: 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 6 10 Sample Output 1: 6 7 8 10  

Search In BST

Given a BST and an integer k. Find if the integer k is present in the given BST or not. You have to return true, if a node with data k is present, return false otherwise. Note: Assume that BST contains all unique elements. Input Format: The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line of input contains an integer, that denotes the value of k. Output Format: The first and only line of output contains a boolean value. Print true, if node with data k is present, print false otherwise. Constraints: Time Limit: 1 second Sample Input 1 : 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 2 Sample Output 1 : true Sample Input 2 : 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 12 Sample Output 2 : false  

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