Advanced Calculator | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryCreate an advanced calculator | ||
DescriptionThe goal of this challenge is to create an advanced calculator. Pi Pi number e Euler's number sqrt() Square root cos() Cosine sin() Sine tan() Tangent lg() Decimal logarithm ln() Natural logarithm 1 () Brackets 2 || Absolute value, e.g. |-5 - 10| 3 ! Factorial 4 - Unary minus 5 ^ Exponent 6 *, /, mod Multiply, Divide, Modulus, e.g. 5 mod 2 = 1 (left-to-right precedence) 7 +, - Add, Subtract (left-to-right precedence)
Only integers will be provided to the modulus function. Each number in input expression is greater than -20,000 and less than 20,000. Each output number is greater than -20,000 and less than 20,000. If output number is a float number it should be rounded to the 5th digit after the dot. E.g 14.132646 gets 14.13265, 14.132644 gets 14.13264, 14.132645 gets 14.13265.
If output number has less than 5 digits after the dot you don't need to add zeros. E.g. you need to print 16.34 (and not 16.34000) if the answer is 16.34. And you need to print 16 (and not 16.00000) if the answer is 16.
Note: do NOT use any kind of eval function. | ||
InputYour program should read lines from standard input. Each line is one test case. Each line contains a mathematical expression. | ||
OutputPrint out the result of the calculation. | ||
Climbing Stairs 2 | ||
| Difficulty: Hard | Test Cases: 12 | Published: 2022-01-27 |
SummaryCount the number of ways to climb to the top of a staircase | ||
DescriptionYou are climbing a staircase with n total stairs. Every time you take a step, you can climb either 1 or 2 stairs. In how many distinct ways can you climb to the top? | ||
InputYour program should read lines from standard input. Each line contains a positive integer n which is the total number of stairs: n <= 1000 For example: 3
| ||
OutputPrint out a single integer which is the number of different ways to climb to the top of the staircase. For example: 3 See the attachments tab for an image representation of 3 ways to climb on top of the staircase having 3 steps. | ||
Attachments | ||
Closest Pair | ||
| Difficulty: Hard | Test Cases: 3 | Published: 2012-10-08 |
SummaryGiven a set of points in a two dimensional space, you will have to find the distance between the closest two points. | ||
DescriptionCredits: Programming Challenges by Steven S. Skiena and Miguel A. Revilla | ||
InputYour program should read lines of text from standard input. The input consists of several sets of input. Each set of input starts with an integer N (0<=N<=10000), which denotes the number of points in this set. The next N lines contain the coordinates of two-dimensional points. For a set of 5 points, there would be 6 lines. The first line would contain the number 5 and then next 5 lines would be the points. The first of the two numbers denotes the X-coordinate and the latter denotes the Y-coordinate. The input is terminated by a line containing only 0. This line should not be processed. The value of the coordinates will be less than 40000 and non-negative. | ||
OutputFor each set of input print a single line to standard output containing a floating point number (with four digits after the decimal point) which denotes the distance between the closest two points. If there are no such two points in the input whose distance is less than 10000, print the line INFINITY. | ||
Computer Terminal | ||
| Difficulty: Hard | Test Cases: 2 | Published: 2014-03-21 |
SummaryPrint text to terminal with control sequences | ||
DescriptionIn this problem you are writing the software for a small terminal with a 10-row, 10-column display (perhaps for a point-of-sale terminal). Rows and columns are numbered 0 through 9. The character that introduces a control sequence is ^ , the circumflex. The character (or in one case, the two characters) immediately following the control sequence introducer will direct your software in performing its special functions. Here is the complete list of control sequences you will need to interpret: | ||
InputYour program should read lines from standard input. Each test case consists of multiple lines. The first line is the number of lines in the test case N. The next N lines contain the symbols described above. | ||
OutputPrint the result of the terminal commands. | ||
Da Vyncy | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryRecreate a document from a set of fragments. | ||
DescriptionPrologue | ||
InputYour program should read lines from standard input. Each line represents a test case. Each line contains fragments separated by a semicolon, which your assistant has painstakingly transcribed from the shreds left by the Illuminati. You may assume that every fragment has length at least 2 and at most 1022 (excluding the trailing newline, which should *not* be considered part of the fragment). | ||
OutputPrint out the original document, reassembled. | ||
Detecting Cycles | ||
| Difficulty: Hard | Test Cases: 3 | Published: 2012-10-08 |
SummaryDetecting loops within a sequence. | ||
DescriptionGiven a sequence, write a program to detect cycles within it. | ||
InputYour program should read lines of text from standard input. Each line will contain a sequence of space delimited numbers, which can have multiple digits (e.g., 12). Ignore empty lines. | ||
OutputFor each line of input, print to standard output the first cycle you find. Ensure that there are no trailing spaces on each line you print. | ||
Discount Offers | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryDetermine optimal pairing of customers with products | ||
DescriptionOur marketing department has just negotiated a deal with several local merchants that will allow us to offer exclusive discounts on various products to our top customers every day. The catch is that we can only offer each product to one customer and we may only offer one product to each customer. | ||
InputYour program should read lines from standard input. Each line is a comma delimited set of customer names followed by a semicolon, and then a comma delimited set of product names. Assume the input is ASCII encoded. | ||
OutputPrint out the maximum total score to two decimal places. | ||
Distinct Subsequences | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryDetermine the number of distinct subsequences within a string. | ||
DescriptionA subsequence of a given sequence S consists of S with zero or more elements deleted. Formally, a sequence Z = z1z2..zk is a subsequence of X = x1x2...xm, if there exists a strictly increasing sequence of indices (i) of X such that for all j=1,2,...k we have Xi = Zj. E.g. Z=bcdb is a subsequence of X=abcbdab with corresponding index sequence <2,3,5,7> | ||
InputYour program should read lines from standard input. Each line contains two comma separated strings. The first is the sequence X and the second is the subsequence Z. | ||
OutputPrint out the number of distinct occurrences of Z in X as a subsequence. | ||
Distinct Triangles | ||
| Difficulty: Hard | Test Cases: 6 | Published: 2013-12-13 |
SummaryFind the number of distinct triangles formed in a graph | ||
DescriptionAlice the archaeologist has just entered the tomb of the Pharaoh. She turns on her flashlight and notices an undirected graph painted on the wall, with V nodes and E edges. Suddenly, the stone door behind her slams shut. Fortunately, Alice knows the way out - she must place N pebbles upon the altar to re-open the door, where N is the number of triangles in the graph. 0--1 | /| |/ | 2--3
N is two in this graph. | ||
InputYour program should read lines from standard input. The input begins with a line containing two integers, V and E (1 <= V, E <= 1000), separated by a space. Then, E lines follow, each containing two integers separated by space, representing two vertices connected by an undirected edge. Each vertex is in the range (0 <= vertex < V). | ||
OutputPrint a line containing the number of distinct triangles formed over three vertices and edges in the graph. | ||
Efficient Delivery | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryLoad your tankers with oil | ||
DescriptionA shipping company is providing oil delivery between two continents using tankers. They’re trying to increase their efficiency by keeping their ships in port to wait for additional oil to prevent setting to sea only partially loaded. | ||
InputYour program should read lines from standard input. Each line contains rows with available tankers in brackets in sorted order, and the amount of oil after a comma. | ||
OutputPrint out all possible variations of efficient delivery in sorted order if the efficient delivery exists. Otherwise, print out the amount of oil to be added. | ||
Find Min | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryFacebook Hacker Cup 2013 problem. | ||
DescriptionCredits: This problem appeared in the Facebook Hacker Cup 2013 Hackathon. | ||
InputYour program should read lines from standard input. Each line contains 6 comma separated positive integers which are the values of n,k,a,b,c,r in order. | ||
OutputPrint out the nth element of m for each test case. | ||
Find My Cousin | ||
| Difficulty: Hard | Test Cases: 5 | Published: 2019-04-02 |
SummarySeek relatives in a tree | ||
DescriptionSearch a family tree for all cousins of the person requested. A cousin is defined as any child of the parent's sibling. In other words, a first cousin in the same generation. For example:
| ||
InputThe first line of input is the name of the person for which to find cousins. Each subsequent line represents a generation (in order) with names separated by a comma. A pipe separates the sets of siblings within each generation. Following the example: There will never be more than two children of a given parent and each generation will be completely described, including empty spaces. | ||
OutputA sorted list of cousins. If the result is empty, print NULL. | ||
Following Integer | ||
| Difficulty: Hard | Test Cases: 8 | Published: 2012-10-08 |
SummaryDetermine the next number in a sequence | ||
DescriptionCredits: This challenge has appeared in a past google competition | ||
InputYour program should read lines of text from standard input. Each line will contain a single integer n < 10^6. | ||
OutputFor each input integer n, print to standard output the next integer in the list, one integer per line. | ||
Hot Pursuit | ||
| Difficulty: Hard | Test Cases: 8 | Published: 2019-11-15 |
SummaryCalculate a travel time for a curvilinear motion | ||
DescriptionA dog is chasing a cat on a large field, with a single big tree growing on it. The cat wants to escape on the tree and runs towards it in a straight line at a fixed speed. The dog is not clever enough to devise a strategy of interception and always runs directly towards the cat at a fixed speed, constantly changing direction as the cat runs to the tree. Given initial positions and velocities of the animals and the position of the tree, calculate if the cat can escape. If not, calculate the time before the dog captures the cat. To escape successfully the cat has to reach the tree before being caught by the dog. If they get to the tree simultaneously the cat is captured. Note that you can simulate curvilinear movement with a series of transitional rectilinear movements. In every such short term update, you need to calculate the cat's displacement in a straight line towards a tree and the dog's displacement in a straight line towards the cat. | ||
InputThe first line contains two real numbers: x and y coordinates of the tree. The second line contains three real numbers: x and y coordinates of the initial position of the cat, and the cat's velocity. The third line contains three real numbers: x and y coordinates of the initial position of the dog, and the dog's velocity. For example: 0.0 -5.0 -15.0 10.0 2.0 15.0 10.0 3.0 All coordinates are in meters and velocities are in meters per second. All real numbers have exactly one digit after the decimal point. | ||
OutputPrint the time in seconds that has passed since the start of the pursuit until the moment when the cat is captured or print Escaped if the cat escapes successfully. Print the time as a real number with exactly one digit after the decimal point. | ||
Lights Out | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2013-12-23 |
SummaryGiven an N by M board of lights and the condition that switching a light toggles all surrounding lights, determine how many switches it will take to turn all the lights off. | ||
DescriptionBob operates all electronic lights at the circus. Lately, there is a defective circuit board that is causing him some problems. The circuit board controls a rectangular box filled with N by M lights, each of which may be on or off depending on the lighting needs of the performers. However, each time he presses the button to toggle a particular light on or off, the circuit board toggles not only that light, but also the lights above, below, to the left, and to the right of the light. .O.. .... OOO. -> .... (toggle the light on row 2, column 2) .O.. ....
OOO. .... .O.. -> .... (toggle the light on row 1, column 2) .... ....
.... ..OO ...O .... .... .O.. -> .O.O -> ..O. -> ...O -> .... .... .... ..O. ..OO ....
(toggle lights as follows: row 1, column 4; row 2, column 3; row 2, column 4; and row 3, column 4)
Bob needs to turn off all the lights. Write a program that prints the minimum number of times he must press a button on the board so that all the lights turn off, or -1 if it is not possible. | ||
InputThe input begins with two integers on a line, N and M (1 <= N,M <= 10), separated by a space. Then, N lines follow, each with M characters either '.' or 'O', indicating a light that is current off or a light that is currently on, respectively. | ||
OutputPrint a line containing the minimum number of clicks that will turn off all the lights, or -1 if it is not possible to turn off all the lights within N*M clicks. | ||
Longest Common Subsequence | ||
| Difficulty: Hard | Test Cases: 3 | Published: 2012-10-08 |
SummaryLCS between two strings. | ||
DescriptionGiven two strings, write a program to determine the longest common subsequence between them. Each string can have a maximum length of 50 characters. Note, this subsequence need not be contiguous. | ||
InputYour program should read lines of text from standard input. Ignore empty lines. Each non-empty line will contain two semicolon-separated strings. You can assume that there is only one unique subsequence per line. | ||
OutputFor each line of input, print to standard output the longest common subsequence. Ensure that there are no trailing spaces on each line you print. | ||
Maximum Flow | ||
| Difficulty: Hard | Test Cases: 11 | Published: 2020-08-21 |
SummaryCalculate the maximum flow in the flow network | ||
DescriptionA flow network is a directed graph where each edge has a certain capacity. Every edge can pass some amount of flow through itself, which doesn't exceed the edge capacity. Write a program to calculate a maximum flow through the flow network. There are N nodes in the graph indexed from 0 to (N - 1). The source node has index 0, the sink node has index (N - 1). The capacity of each edge is greater than zero. | ||
InputThe first line contains a positive integer, the number of nodes in the graph N. Each additional line describes one edge of the graph with three integers. The first two numbers are the indices of adjacent nodes, and the edge is directed from the first vertex in the pair to the second. The last number is the capacity of the edge. For example: 4 0 1 12 0 2 10 1 2 2 1 3 10 2 3 12
| ||
OutputA single integer, which is the maximum possible flow through the network from the source node to the sink node. | ||
Median Filter Root Sequences | ||
| Difficulty: Hard | Test Cases: 7 | Published: 2019-10-04 |
SummaryCount the number of root sequences for the standard median filter | ||
DescriptionIn digital signal processing, a sampled discrete-time signal has the following characteristics defined for a median filter of window width 2N + 1:
Note that constant neighborhoods are not included in edges and impulses. The constant neighborhood preceding the edge must have a value different from the value of the constant neighborhood following the edge. But for the impulse, preceding and following constant neighborhoods must have equal values. The image below illustrates a signal with:
If applying a filter of half-width N = 2:
See the attachments tab for a diagram of this example Write a program that will analyze an input signal by counting the number of constant neighborhoods, edges, and impulses it contains. | ||
InputThe first line contains an integer N which is a filter window half-width. The second line has comma-delimited samples of the input signal, which are integer values. For example: 2 0,0,0,2,2,0,0,0,1,2,2,3,4,5,5,5,6,7,8,5,6,6,6,6,5,4,3,2,1,1,1
| ||
OutputPrint out three comma-delimited numbers:
For example: 5,2,1
| ||
Attachments | ||
Median Filter Signal Oscillations | ||
| Difficulty: Hard | Test Cases: 7 | Published: 2019-11-15 |
SummaryCount the number of signal oscillations for the standard median filter | ||
DescriptionIn digital signal processing, a sampled discrete-time signal has the following characteristics defined for a median filter of window width 2N + 1:
Note that constant neighborhoods are not included in edges and impulses. The constant neighborhood preceding the edge must have a value different from the value of the constant neighborhood following the edge. But for the impulse, preceding and following constant neighborhoods must have equal values. The image below illustrates a signal with:
If applying a filter of half-width N = 2:
See the attachments tab for a diagram of this example Write a program that will analyze an input signal by counting the number of oscillations it contains. | ||
InputThe first line contains an integer number N which is a filter window half-width. The second line has comma-delimited samples of the input signal, which are integer values. For example: 2 0,0,0,2,2,0,0,0,1,2,2,3,4,5,5,5,6,7,8,5,6,6,6,6,5,4,3,2,1,1,1
| ||
OutputPrint out the number of oscillations. For example: 1 | ||
Attachments | ||
Message Decoding | ||
| Difficulty: Hard | Test Cases: 2 | Published: 2012-10-08 |
SummaryDecode an encoded message | ||
DescriptionCredits: This challenge has appeared in a past ACM competition. | ||
InputYour program should read lines of text from standard input. Ecah line consists of a header and a message. The length of the header is limited only by the fact that key strings have a maximum length of 7 (111 in binary). Note that neither 0 nor 1 will occur in the header. If there are multiple copies of a character in a header, then several keys will map to that character. The encoded message contains only 0's and 1's, and it is a legitimate encoding according to the described scheme. That is, the message segments begin with the 3-digit length sequence and end with the appropriate sequence of 1's. The keys in any given segment are all of the same length, and they all correspond to characters in the header. The message is terminated by 000. | ||
OutputFor each data set, print to standard output the decoded message, one message per line. | ||
Minesweeper | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryFind the mines within a M*N matrix. | ||
DescriptionYou will be given an M*N matrix. Each item in this matrix is either a '*' or a '.'. A '*' indicates a mine, whereas a '.' does not. The objective of the challenge is to output a M*N matrix where each element contains a number (except the positions which actually contain a mine which will remain as '*') which indicates the number of mines adjacent to it. Notice that each position has at most 8 adjacent positions e.g. left, top left, top, top right, right, ... * * . . . . . . . . . * . . .
becomes * * 1 0 0 3 3 2 0 0 1 * 1 0 0 | ||
InputYour program should read lines from standard input. Each line contains M,N, a semicolon and the M*N matrix in row major form. | ||
OutputPrint out the new M*N matrix (in row major form) with each position(except the ones with the mines) indicating how many adjacent mines are there. | ||
Minimum Path Sum | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryCalculate the minimum sum of a path through a matrix. | ||
DescriptionYou are given an n*n matrix of integers. You can move only right and down. Calculate the minimal path sum from the top left to the bottom right | ||
InputYour program should read lines from standard input. Each test case consists of multiple lines. The first line will have the value of n (the size of the square matrix). This will be followed by n rows of the matrix. (Integers in these rows will be comma delimited). | ||
OutputPrint out the minimum path sum for each matrix. | ||
Package Problem | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryPut as many things into a package as possible | ||
DescriptionYou want to send your friend a package with different things. | ||
InputYour program should read lines from standard input. Each line contains the weight that a package can take (before the colon) and the list of things you need to pick from. Each thing is enclosed in parentheses where the 1st number is a thing's index number, the 2nd is its weight and the 3rd is its cost. | ||
OutputFor each set of things produce a list of things (their index numbers separated by comma) that you put into the package. If none of the items will fit in the package, print a hyphen (-). | ||
Palindromic Ranges | ||
| Difficulty: Hard | Test Cases: 6 | Published: 2012-10-08 |
SummaryFind out a range of palindromic numbers | ||
DescriptionA positive integer is a palindrome if its decimal representation (without leading zeros) is a palindromic string (a string that reads the same forwards and backwards). For example, the numbers 5, 77, 363, 4884, 11111, 12121 and 349943 are palindromes. | ||
InputYour program should read lines of text from standard input. Each line will contain two positive integers, L and R (in that order), separated by a space. | ||
OutputFor each line of input, print out the number of interesting subranges of [L,R] | ||
Play with DNA | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryBeware of what you're outputting | ||
DescriptionThe following problem is related to bioinformatics tasks. In order to help in DNA research you will be asked to write an algorithm to find all occurrences of the DNA segment in a given DNA string. But that would be too easy for you. So please write an algorithm to find all occurrences of segment S in DNA string, with maximum allowed mismatches of M. By mismatch, we mean the minimum of total number of substitutions, deletions, insertions necessary to perform on the DNA slice in order to completely match the given segment. You need to look at the DNA slices with the same length as a given pattern. E.g. the segments 'AGTTATC' -> 'AGTATGC' have only 2 mismatches. | ||
InputYour program should read lines from standard input. Each line contains a segment of DNA, the maximum allowed mismatches M and the DNA string, separated by whitespace. | ||
OutputPrint out all the occurrences of a segment S in the DNA string in order from the best match, separated by whitespace, taking into account the number of allowed mismatches. If there are several segments with the equal matches, print them in alphabetical order. Print out 'No match' if there is no such occurrence. | ||
Poker Hands | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryCompare two poker hands. | ||
DescriptionIn the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way: | ||
InputYour program should read lines from standard input. Each line contains 2 hands (left and right). Cards and hands are separated by space. | ||
OutputPrint out the name of the winning hand or "none" if the hands are equal. | ||
Prefix expressions | ||
| Difficulty: Hard | Test Cases: 4 | Published: 2012-10-08 |
SummaryEvaluating a prefix expression. | ||
DescriptionWrite a program to evaluate prefix expressions. | ||
InputYour program should read lines of text from standard input. Each line is a prefix expression with tokens delimited by a single space. You may assume that the only operators appearing in the input are '+', '*', and '/'. | ||
OutputFor each input expression, evaluate it and print to standard output the result, one result per line. | ||
Register Allocation | ||
| Difficulty: Hard | Test Cases: 11 | Published: 2019-10-04 |
SummaryCalculate the minimum number of registers required to allocate variables | ||
DescriptionIn computer architecture, a processor register is a small storage element which provides the fastest way to access data. When the computer program is compiled, the compiler must assign variables holding the data to a limited number of registers to improve the execution time. This process is called a register allocation. One approach to solving the register allocation problem is to use an interference graph. This is a graph in which vertices represent variables and edges connect pairs of variables that are used in the program at the same time. With this representation, the register allocation is reduced to graph coloring, so that adjacent vertices have different colors. Each color used in the graph represents a single register. If k colors are used to color the whole graph then k registers are required to cache the given set of variables. Imagine a simple case where at most ten variables are used in the program and the processor has only four registers. Write a program to calculate the minimum number of registers required to allocate all variables. | ||
InputAn adjacency list of the interference graph. Each line describes one vertex of the graph, which is a space-delimited list of variable names. The first element of the list is the name of the current variable, and all other elements are the variables that are adjacent to the current. For example: a b c d b a c e c a b d e d a c e e b c d See the attachments tab for a diagram of this example | ||
OutputThe number of registers required for the allocation of all variables or Overflow when more than four registers are required. | ||
Attachments | ||
Repeated Substring | ||
| Difficulty: Hard | Test Cases: 5 | Published: 2012-10-08 |
SummaryFind the longest repeated substring in a given text | ||
DescriptionYou are to find the longest repeated substring in a given text. Repeated substrings may not overlap. If more than one substring is repeated with the same length, print the first one you find.(starting from the beginning of the text). NOTE: The substrings can't be all spaces | ||
InputYour program should read lines of text from standard input. Each line contains a string. | ||
OutputFor each set of input print a single line to standard output which is the longest repeated substring. If there is none, print out the string NONE. | ||
Seat your team | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryPlace the employees in a new office | ||
DescriptionYour team is moving to a new office. In order to make it feel comfortable on a new place you decided to give the possibility to pick the places where they want to sit. After the team visited the new office, each team member gave you a list of working places that he/she would like to occupy. Your goal is to determine a possibility of making all of your team members feel comfortable according to those lists. | ||
InputYour program should read lines from standard input. Each line contains an integer N of available places in the office as the first number, and the lists of places that have been chosen by each team member. These lists are enclosed by square brackets. | ||
OutputPrint out the simple "Yes" or "No" answer for the following question: "Is there a possibility to make all of your team members feel comfortable at the new office?". | ||
Shelter for Rabbits | ||
| Difficulty: Hard | Test Cases: 12 | Published: 2020-08-21 |
SummaryCalculate a maximum number of rabbits that can find a shelter | ||
DescriptionN rabbits live in the forest. Every evening, before the sunset, the rabbits must hide in holes for the night to avoid danger in the darkness. There are M holes scattered throughout the forest. Each hole has a certain capacity, so a limited number of rabbits can use it as a shelter. Given the conditions before sunset, calculate the maximum number of rabbits that can hide for the night. Assume that every rabbit runs at a constant speed and can cover up to S meters before darkness. In other words, a rabbit can hide in time if the distance between rabbit and hole is less than or equal to S. | ||
InputThe first line of input contains three positive numbers N, M, and S:
N and M are integers. S is a real number. The next N lines contain two real numbers each: x and y coordinates of each rabbit. The last M lines have three numbers each: x and y coordinates followed by the capacity of each hole. Real numbers have exactly one digit after the decimal point. All numbers are separated with a space character. For example: 3 2 5.0 0.0 6.0 0.0 2.0 8.0 2.0 4.0 4.0 1 10.0 4.0 2
| ||
OutputA single integer showing the maximum number of rabbits that can hide safely. | ||
Single Destination - Shortest Path | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2019-11-15 |
SummaryFind the shortest paths to one destination vertex from other vertices in the directed graph | ||
DescriptionWrite a program to find the shortest paths in a directed graph to one destination vertex from all other vertices where the destination is reachable. Edges of the graph have no weights and the length of the path is the number of edges between two vertices. If the destination vertex is not reachable from some other vertex, the path between them is equal to infinity. Every vertex in the graph has a unique string name consisting of alphanumeric characters (UTF-8). | ||
InputThe first line of input contains the name of the destination vertex. The second line has a list of edges. Every edge is described as a comma-delimited pair of names of adjacent vertices. The edge is directed from the first vertex in the pair to the second. Edges are separated with a semicolon in the list. For example: F A,B;B,C;C,A;D,B;C,D;D,E;E,F;F,B;F,H;G,C;G,E;G,F;G,H See the image of this graph in the attachments tab. | ||
OutputPrint each possible path on a single line as a vertex name and a positive integer that is the length of the shortest path from this vertex to the destination. Separate the vertex name and path length with a space. If there is no path from the vertex, print INF instead of the integer length. Sort lines in alphabetical order of vertex names and do not include the destination vertex in the output. For example: A 5 B 4 C 3 D 2 E 1 G 1 H INF
| ||
Attachments | ||
Skyscrapers | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryOutline skyscrapers in a city | ||
DescriptionYou are to design a program which helps you in drawing the skyline of a city. You are given the locations of the buildings represented by triples (L, H, R) where L and R are left and right coordinates of the building and H is the height of the building. All buildings are rectangular in shape and they are standing on a very flat surface. See the attachments tab for diagrams showing the image representation of buildings and the skyline of the city. On the first diagram the buildings are represented by the following triples: (1,2,3); (2,4,6); (4,5,5); (7,3,11); (9,2,14); (13,7,15); (14,3,17) The drawing line as shown on the second diagram is described by the following sequence: 1 2 2 4 4 5 5 4 6 0 7 3 11 2 13 7 15 3 17 0
| ||
InputYour program should read lines from standard input. Each line is one test case. Each test case will contain the list of triples semicolon separated.
| ||
OutputThe output must describe the drawing line as a vector where X is an x-coordinate of a point where the line is changing its direction from horizontal to vertical, and H is a height of the vertical line. You're drawing continuously by starting at the bottom of the first left building and finishing at the bottom of the right building. So for each test case print out the drawing line in a way as it is shown below. | ||
Attachments | ||
Spiral Printing | ||
| Difficulty: Hard | Test Cases: 4 | Published: 2012-10-08 |
SummaryPrint out a 2D array in spiral order | ||
DescriptionWrite a program to print a 2D array (n x m) in spiral order (clockwise) | ||
InputYour program should read lines of text from standard input. Each line contains three semicolon-delimited items. The first is 'n'(rows), the second is 'm'(columns), and the third is a single space delimited list of characters/numbers in row major order. | ||
OutputPrint out the matrix in clockwise fashion, one per line, space delimited. | ||
Strassen Matrix Multiplication | ||
| Difficulty: Hard | Test Cases: 7 | Published: 2019-04-02 |
SummaryMultiply matrices and find components of Strassen Algorithm | ||
DescriptionThe Strassen Algorithm is a divide and conquer algorithm that reduces matrix multiplication to a series of operations that use block matrices of a smaller size. Let X, Y be two square matrices of size n x n. We want to calculate their product Z.
Next step is to calculate 7 temporary components: And finally, these 7 components can be combined with additions and subtractions to form the final result Z: Write a program to calculate the components | ||
InputTwo lines with space-delimited elements of X and Y matrices in row-major order. All elements are integers. The first line contains X, the second line contains Y. For example: 5 5 2 6 2 6 6 2 1 7 3 9 1 1 8 8 2 8 7 1 4 4 0 1 8 7 0 7 6 1 4 6
| ||
OutputWrite matrix product For example: 82 80 59 60 88 84 22 62 108 66 43 83 118 76 39 106 15 -55 -10 -42 44 115 32 104 72 96 54 108 36 -30 64 -32 128 260 132 290 -38 -35 -76 -70 28 26 29 34
| ||
String List | ||
| Difficulty: Hard | Test Cases: 6 | Published: 2012-10-08 |
SummaryCreate a new string from constituent alphabets | ||
DescriptionCredits: Challenge contributed by Max Demian. | ||
InputYour program should read lines of text from standard input. Each line is in the format N,S where N is a positive integer and S is a string. | ||
OutputFor each line of input, print to standard output a comma separated list of all of the possible ways to write a string of length N from the characters in string S, one list per line. | ||
String Permutations | ||
| Difficulty: Hard | Test Cases: 2 | Published: 2012-10-08 |
SummaryPrint out all permutations of a string. | ||
DescriptionWrite a program that finds all the permutations of a string. | ||
InputYour program should read lines of text from standard input. | ||
OutputFor each line of input, print to standard output all permutations of the string, comma separated, in alphabetical order, one output string per line. | ||
String Searching | ||
| Difficulty: Hard | Test Cases: 8 | Published: 2012-10-08 |
SummaryDetermine if substring match exists. | ||
DescriptionYou are given two strings. Determine if the second string is a substring of the first. The second string may contain an asterisk (*) which should be treated as a wild card that matches zero or more characters. The asterisk can be escaped by a \ char in which case it should be interpreted as a literal * character. The strings can contain English letters and numbers, *, and \. | ||
InputYour program should read lines of text from standard input. Each line will contain two strings separated by a comma. | ||
OutputFor each line of input, if the second string is a substring of the first, print "true" (lowercase). Otherwise print "false" (lowercase), one per line. | ||
String Substitution | ||
| Difficulty: Hard | Test Cases: 2 | Published: 2012-10-08 |
SummaryCreate a new string by replacing substrings within it | ||
DescriptionCredits: This challenge was contributed by Sam McCoy | ||
InputYour program should read lines of text from standard input. Each line will contain a string, then a semicolon, and then a list of comma separated strings. | ||
OutputFor each line of input, print out the string after substitutions have been made. Here are the transitions for the above example: 10011011001 => 10100111001 [replacing 0110 with 1001] => 10100110 [replacing 1001 with 0] => 11100110 [replacing 10 with 11] => 11100110 | ||
Strongly Connected Components | ||
| Difficulty: Hard | Test Cases: 12 | Published: 2020-11-19 |
SummaryFind strongly connected components of a directed graph | ||
DescriptionIn graph theory, a directed graph is called strongly connected if there is a path in both directions between any two vertices within it. The strongly connected component (SCC) of the directed graph is its subgraph that is strongly connected, and is maximal with this property: no additional edges or vertices from the original graph can be included in the subgraph without breaking its property of being strongly connected. In other words, the SCC is a subgraph of maximum size such that for any pair of its vertices u and v there is a path from u to v and also a path from v to u. Write a program that finds the strongly connected components of a directed graph with N vertices. Vertices are indexed with continuous integer numbers from 0 to ( N - 1). See the attachments tab for an example of the graph with marked SCCs. | ||
InputThe first line contains a positive integer N, the number of vertices in the graph. Each additional line describes one edge of the graph with two space-delimited integer indices. The edge is directed from the vertex with the first index to the vertex with the second index. For example: 6 2 4 4 3 5 4 1 0 3 2 0 5 1 2 5 1
| ||
OutputPrint strongly connected components of the given graph, one component per line. Every strongly connected component should be described as a space-delimited list of vertex indices it consists of. Indices in the list must be sorted in ascending order. The lines must also be printed in ascending order. The first line describes the component that starts with vertex 0, next line describes the component that starts with the next higher index, and so on. For example: 0 1 5 2 3 4
| ||
Attachments | ||
Strongly Connected Components - Simple | ||
| Difficulty: Hard | Test Cases: 12 | Published: 2020-11-19 |
SummaryFind strongly connected components of the directed graph | ||
DescriptionIn graph theory, a directed graph is called strongly connected if there is a path in both directions between any two vertices within it. The strongly connected component (SCC) of the directed graph is its subgraph that is strongly connected, and is maximal with this property: no additional edges or vertices from the original graph can be included in the subgraph without breaking its property of being strongly connected. In other words, the SCC is a subgraph of maximum size such that for any pair of its vertices u and v there is a path from u to v and also a path from v to u. Write a program that finds the strongly connected components of a directed graph with N vertices. Vertices are indexed with continuous integer numbers from 0 to (N - 1) . One of the possible methods to find SCCs of the graph G is a Kosaraju's algorithm. There are three steps in it:
dfs_loop is a loop that starts a depth-first search (DFS) from unexplored vertices of the graph. The difference between steps 2 and 3 is only in the order of the vertex processing and some bookkeeping. The second step tracks finishing order and the third step tracks the source node index for every DFS call (the index of the node from which the DFS starts). Every call to the DFS in the third step explores nodes of one SCC and dfs_loop discovers all SCCs of the graph one by one. The pseudocode of the dfs_loop and dfs functions can be represented like so: dfs_loop(graph G) finish_counter = 0 dfs_source = 0 for not explored node u in graph dfs_source = u dfs(graph, u)
dfs(graph G, node u) mark u as explored set SCC index of the node u = dfs_source for each not explored node v reachable from u dfs(graph, v)
finish_counter++ set f(u) = finish_counter Let's look at how to apply the Kosaraju's algorithm to find SCCs of the graph from the attachments tab. The first step, as described earlier, is to construct a reversed graph. The dfs_loop of the second step triggers an initial DFS from node 5 and that DFS explores reachable nodes 0 and 1 in the reversed graph. The second iteration of dfs_loop starts the DFS from node 4 (unexplored node with the highest index) and explores nodes 2 and 3. The order in which the DFS finishes the nodes exploration in step 2 of the algorithm is {1, 0, 5, 3, 2, 4}. The third step starts DFS from node 4, which is the last processed node in the previous step. This DFS run discovers nodes 3 and 2, they form the first SCC {4, 3, 2}. Then dfs_loop proceeds with DFS starting from node 5, and explores nodes 1 and 0. So the second discovered SCC is {5, 1, 0}. | ||
InputThe first line contains a positive integer N, the number of vertices in the graph. Each additional line describes one edge of the graph with two space-delimited integer indices. The edge is directed from the vertex with the first index to the vertex with the second index. For example: 6 2 4 4 3 5 4 1 0 3 2 0 5 1 2 5 1
| ||
OutputPrint out strongly connected components of the given graph, one component per line. Every strongly connected component should be described as a space-delimited list of vertex indices it consists of. Indices in the list must be sorted in ascending order. The lines must also be printed in ascending order. The first line describes the component that starts with vertex 0, next line describes the component that starts with the next higher index, and so on. For example: 0 1 5 2 3 4
| ||
Attachments | ||
Symmetry of a Binary Tree | ||
| Difficulty: Hard | Test Cases: 12 | Published: 2019-11-15 |
SummaryCheck whether a binary tree is symmetric | ||
DescriptionWrite a program to check whether a binary tree is symmetric or not. A tree is symmetric if its data and shape remain unchanged when it is reflected around the root node. Consider two examples below.
__7__ / 5 5 / \ / 4 9 9 4
6 / 3 3 / / 8 8
| ||
InputA string containing the pre-order traversal of the binary tree to be inspected. The nodes are traversed in the following order:
Any absent child (NULL pointer) of a given node is marked with a # character in the traversal. The input string is a space-delimited mix of integers and # characters. There will be no trailing # characters in the string, it will always end with the value of the last node in the tree traversal. Tree nodes will contain integer values only. The first example above, which is symmetric, would be described as: 7 5 4 # # 9 # # 5 9 # # 4
| ||
OutputPrint True if the binary tree is symmetric. Otherwise, print False. | ||
Telephone Words | ||
| Difficulty: Hard | Test Cases: 4 | Published: 2012-10-08 |
SummaryPrint out the words corresponding to a telephone number | ||
DescriptionGiven a 7 digit telephone number, print out all the possible sequences of letters that can represent the given telephone number. Note that in a standard phone keypad 0 and 1 do not have any letters associated with them. All 0's and 1's in the telephone number should be retained in the sequence of letters. You may use the following mapping between numbers and characters 0=>0, 1=>1, 2=>abc, 3=>def, 4=>ghi, 5=>jkl, 6=>mno, 7=>pqrs, 8=>tuv, 9=>wxyz | ||
InputYour program should read lines from standard input. Each line contains a 7 digit telephone number. | ||
OutputPrint to standard output the words that can produce the telephone number, alphabetically sorted and comma delimited. | ||
Text Dollar | ||
| Difficulty: Hard | Test Cases: 9 | Published: 2012-10-08 |
SummaryPrint out the text dollar amount of a given quantity | ||
DescriptionCredits: This challenge has been authored by Terence Rudkin | ||
InputYour program should read lines of text from standard input. Each line contains a positive integer. | ||
OutputFor each set of input print a single line to standard output which is the english textual representation of that integer. The output should be unspaced and in CamelCased. Always assume plural quantities. You can also assume that the numbers are < 1000000000 (1 billion). In case of ambiguities eg. 2200 could be TwoThousandTwoHundredDollars or TwentyTwoHundredDollars, always choose the representation with the larger base i.e. TwoThousandTwoHundredDollars. | ||
Text to Number | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryConvert English text representation of a number to a decimal number | ||
DescriptionYou have a string which contains a number represented as English text. Your task is to translate these numbers into their integer representation. The numbers can range from -999,999,999 to 999,999,999. The following is an exhaustive list of English words that your program must account for: negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million
| ||
InputYour program should read lines from standard input. Each line contains an English of a number. | ||
OutputPrint the decimal representation of the number. | ||
Transport Network - Minimum Cut | ||
| Difficulty: Hard | Test Cases: 7 | Published: 2019-10-04 |
SummaryFind a minimum cut of the graph representing the transport network | ||
DescriptionIn a strategy game, a player's base has a transportation network, which is used for interactions between all the base facilities. While the player is constructing and developing their base, their opponent is planning an attack. The first step in this attack is to determine the weakest link between the facilities of the base. Then, attack the weak spot to cut the base into two pieces. Write a program to find the most vulnerable part of the transportation network. The number of roads connecting these two pieces must be the minimum possible. The transport network consists of nodes representing buildings and roads that connect nodes. Every building has an integer index from 0 to n assigned to it. | ||
InputEach line of input describes the connections of a single building with others in the network using a space-delimited list of integer indices. The first element of the list is the index of the current building, and all other elements are the indices of the buildings that are directly connected by roads with the current building. For example: 0 1 5 1 0 2 5 2 1 3 4 3 2 4 4 2 3 5 0 1 The first row means that the building with index 0 is connected to buildings 1 and 5. The second row describes that there are roads from building 1 to buildings 0, 2, and 5. See the attachments tab for a diagram of this example | ||
OutputWrite out two space-delimited, sorted lists representing the complete result of the network cut. The first line lists the nodes of the network containing the node with index 0. The second line describes the other side of the split. For example: 0 1 5 2 3 4
| ||
Attachments | ||
Type Ahead | ||
| Difficulty: Hard | Test Cases: 4 | Published: 2012-10-08 |
SummaryBuilding a type ahead feature | ||
DescriptionYour task is to build a type-ahead feature for an upcoming product. | ||
InputYour program should read lines of text from standard input. Each line contains a number followed by a string, separated by a comma. The number is the n-gram length. The string is the text from the user. You will be predicting the text following this string. | ||
OutputFor each line of input print a single line to standard output which is the predictions for what the user is going to type next. | ||
Ugly Numbers | ||
| Difficulty: Hard | Test Cases: 11 | Published: 2012-10-08 |
SummaryCount the number of expressions that can be created from a number | ||
DescriptionCredits: This challenge has appeared in a google competition. | ||
InputYour program should read lines of text from standard input. Each line will consist of a non-empty string containing only the characters '0' through '9'. Each string is no more than 13 characters long. | ||
OutputFor each input string, print to standard output the number of expressions that evaluate to an ugly number, one number per line. | ||
Word Search | ||
| Difficulty: Hard | Test Cases: 10 | Published: 2014-03-20 |
SummaryFind if a word exists in a grid | ||
DescriptionGiven a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are those directly neighboring either horizontally or vertically. The same letter cell may not be used more than once. A B C E S F C S A D E E
| ||
InputYour program should read lines from standard input. Each line contains a word. | ||
OutputPrint out True if the word exists in the board, False otherwise. | ||