Posts

Showing posts from August, 2020

Balanced Paranthesis

Given a string expression, check if brackets present in the expression are balanced or not. Brackets are balanced if the bracket which opens last, closes first. You need to return true if it is balanced, false otherwise. Note: This problem was asked in initial rounds in Facebook Note:- I try to solve this problem using Linked List, python Queue, and List Sample Input 1 : { a + [ b+ (c + d)] + (e + f) } Sample Output 1 : true Sample Input 2 : { a + [ b - c } ] Sample Output 2 : false

Code : Stack Using LL

You need to implement a Stack class using a linked list. The data members should be private. Implement the following public functions : 1. Constructor - Initialises the data member (i.e. head to null). 2. push : This function should take one argument of type T and has return type void. This function should insert an element in the stack. Time complexity should be O(1). 3. pop : This function takes no input arguments and has return type T. This should removes the last element which is entered and return that element as an answer. Time complexity should be O(1). 4. top : This function takes no input arguments and has return type T. This should return the last 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 stack is empty or not. Return true or false.

Horner rule

Given a polynomial of the form c n x n  + c n-1 x n-1  + c n-2 x n-2  + … + c 1 x + c 0   and a value of x, find the value of polynomial for a given value of x. Here  c n , c n-1 , ..  are integers (may be negative) and n is a positive integer.

Merge Two linked lists

Level MEDIUM Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Try solving without reverse, with O(1) auxiliary space (in-place) and only one traversal of both lists. You just need to return the head of the new linked list, don't print the elements. Input format : Line 1 : Linked list 1 elements of length n (separated by space and terminated by -1) Line 2 : Linked list 2 elements of length m (separated by space and terminated by -1) Output format : Merged list elements (separated by space) Constraints : 1 <= n, m <= 10^4 Sample Input : 2 5 8 12 -1 3 6 9 -1 Note: -1 at the end of input is just a terminator representing the end of the linked list. This -1 is not part of the linked list. Size of 1st linked list is 4 and that of 2nd linked list is 3. Sample Output : 12 9 8 6 5 3 2

013. Resistors in Parallel

013. Resistors in Parallel time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output When two resistors with resistances   a   and   b   are parallel in an electronic circuit, their equivalent resistance is calculated as ( a   *   b ) / ( a   +   b ). Given the resistances of two resistors, calculate their equivalent resistance. Input The first line of input contains a single positive integer   a : the resistance of the first resistor. The second line of input contains a single positive integer   b : the resistance of the second resistor. Output Output a single positive decimal number   r : the equivalent resistance of the two resistors, calculated using the above formula. Examples input Copy 60 40 output Copy 24.0 input Copy 80 70 output Copy 37.333333333333336

012. Easy Exponentials

012. Easy Exponentials time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In the last contest, you were asked to find the last digit of a very large number. This time, you are asked to find the entirety of a much smaller number: your task is to find the first number taken to the power of second number. A brute force approach does work here. Input The only line of input contains two space-separated integers     n   and   k , 1 <=   n ,   k   <= 10. Output Output a single integer: the number   𝑛 𝑘 n k . Example input Copy 3 5 output Copy 243

011. Ski Sum

011. Ski Sum time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Your favorite ski resort has a certain number of easy trails, more difficult trails, most difficult trails, and expert-only trails. Given these values, find how many trails there are at the ski resort. Note that the same trail will never be two different difficulties. Input The only line of input contains 4 space-separated integers, each representing the number of easy trails, more difficult trails, most difficult trails, and expert-only trails, respectively. Output Print one positive integer: the total number of ski trails at the mountain. Example input Copy 20 16 13 6 output Copy 55

010. Points per Game

010. Points per Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Your favorite basketball player is the leading scorer in the country, and you want to know exactly how good they are. Points per game is measured as points divided by games played, and is used to rank leading scorers in basketball. Input The first line of input contains a positive integer   p   indicating the total points scored by the player so far during the season. The second line of input contains a positive integer   g   indicating the total games played by the player so far during the season. Output Output one decimal number   ppg , the player's current points per game. This value can be calculated as   p   /   g Examples input Copy 78 7 output Copy 11.142857142857142 input Copy 89 15 output Copy 5.933333333333334

009. Hello CodeRams

009. Hello CodeRams time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A hello world program is an essential starting point for all coders. In this problem, write a program to print out "Hello CodeRams". In Python, you can print text using the print command. So, for this problem, type   print("Hello CodeRams") . You can download Python   here . Output Output "Hello CodeRams" Example input Copy there is no input for this problem output Copy Hello CodeRams

008. N-Dimensional Distance

008. N-Dimensional Distance time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Calculate the distance between two points of a specified dimension. The common distance formula still applies to higher dimensional points with a small modification. You will input two points and display the distance between them as you normally would with the distance formula. The n-dimensional distance formula can be expressed as 𝑑 ( 𝑝 , 𝑞 ) = ( 𝑞 1 − 𝑝 1 ) 2 + ( 𝑞 2 − 𝑝 2 ) 2 + . . . + ( 𝑞 𝑛 − 𝑝 𝑛 ) 2 ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ √ d ( p , q ) = ( q 1 − p 1 ) 2 + ( q 2 − p 2 ) 2 + . . . + ( q n − p n ) 2 or in other terms: ∑ 𝑛 𝑖 = 1 ( 𝑞 𝑖 − 𝑝 𝑖 ) 2 ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ √ = 𝑑 ∑ i = 1 n ( q i − p i ) 2 = d Remember that   math.sqrt   will calculate the square root of a number (you also have to add the line   import math   at the top of the program).

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