Posts

Showing posts from July, 2020

K Palindrome

https://app.codesignal.com/interview-practice/task/x3rJpdZGEcjmYtDqv/description A string is a  k-palindrome  if it can be transformed into a palindrome by removing any amount of characters from  0  to  k . Your task is to determine whether the given string  s  is a  k-palindrome . Example For  s = "abrarbra"  and  k = 1 , the output should be kpalindrome(s, k) = true . You can remove one letter,  'r' , to obtain the string  "abrarba" , which is a palindrome. For  s = "adbcdbacdb"  and  k = 2 , the output should be kpalindrome(s, k) = false . Input/Output [execution time limit] 3 seconds (java) [input] string s A string containing only lowercase English letters. Guaranteed constraints: 3 ≤ s.length ≤ 100 . [input] integer k Guaranteed constraints: 1 ≤ k ≤ 20 . [output] boolean Return  true  if it is possible to delete at most  k  letters from a string  s  in order to make it palindrome. Return  false  otherwise. ---

Reverse in Parenthesis

https://app.codesignal.com/arcade/intro/level-3/9DgaPsE2a7M6M2Hu6 Write a function that reverses characters in (possibly nested) parentheses in the input string. Input strings will always be well-formed with matching  () s. Example For  inputString = "(bar)" , the output should be reverseInParentheses(inputString) = "rab" ; For  inputString = "foo(bar)baz" , the output should be reverseInParentheses(inputString) = "foorabbaz" ; For  inputString = "foo(bar)baz(blim)" , the output should be reverseInParentheses(inputString) = "foorabbazmilb" ; For  inputString = "foo(bar(baz))blim" , the output should be reverseInParentheses(inputString) = "foobazrabblim" . Because  "foo(bar(baz))blim"  becomes  "foo(barzab)blim"  and then  "foobazrabblim" . Input/Output [execution time limit] 3 seconds (java) [input] string inputString A string consisting of lowercase English letters and the cha

Almost Increasing Sequence

https://app.codesignal.com/arcade/intro/level-2/2mxbGwLzvkTCKAJMG/solutions Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. Note:  sequence  a 0 ,  a 1 , ...,  a n  is considered to be a strictly increasing if  a 0  < a 1  < ... < a n . Sequence containing only one element is also considered to be strictly increasing. Example For  sequence = [1, 3, 2, 1] , the output should be almostIncreasingSequence(sequence) = false . There is no one element in this array that can be removed in order to get a strictly increasing sequence. For  sequence = [1, 3, 2] , the output should be almostIncreasingSequence(sequence) = true . You can remove  3  from the array to get the strictly increasing sequence  [1, 2] . Alternately, you can remove  2  to get the strictly increasing sequence  [1, 3] . Input/Output [execution time limit] 3 seconds (java) [input] array.integer seque

Possible Sums

https://app.codesignal.com/interview-practice/task/rMe9ypPJkXgk3MHhZ/description You have a collection of coins, and you know the values of the  coins  and the  quantity  of each type of coin in it. You want to know how many distinct sums you can make from non-empty groupings of these coins. Example For  coins = [10, 50, 100]  and  quantity = [1, 2, 1] , the output should be possibleSums(coins, quantity) = 9 . Here are all the possible sums: 50 = 50 ; 10 + 50 = 60 ; 50 + 100 = 150 ; 10 + 50 + 100 = 160 ; 50 + 50 = 100 ; 10 + 50 + 50 = 110 ; 50 + 50 + 100 = 200 ; 10 + 50 + 50 + 100 = 210 ; 10 = 10 ; 100 = 100 ; 10 + 100 = 110 . As you can see, there are  9  distinct sums that can be created from non-empty groupings of your coins. Input/Output [execution time limit] 3 seconds (java) [input] array.integer coins An array containing the values of the coins in your collection. Guaranteed constraints: 1 ≤ coins.length ≤ 20 , 1 ≤ coins[i] ≤ 10 4 . [input] array.integer quantity An array contai

Circle Of Numbers

Image
https://app.codesignal.com/tournaments/PmmyGsjEkvJbYK2XF/D Consider integer numbers from  0  to  n - 1  written down along the circle in such a way that the distance between any two neighboring numbers is equal (note that  0  and  n - 1  are neighboring, too). Given  n  and  firstNumber , find the number which is written in the radially opposite position to  firstNumber . Example For  n = 10  and  firstNumber = 2 , the output should be circleOfNumbers(n, firstNumber) = 7 . Input/Output [execution time limit] 3 seconds (java) [input] integer n A positive  even  integer. Guaranteed constraints: 4 ≤ n ≤ 20 . [input] integer firstNumber Guaranteed constraints: 0 ≤ firstNumber ≤ n - 1 . [output] integer ---

Most Frequent Sum

Image
https://app.codesignal.com/interview-practice/task/9i2KvkNRzncy7Bzia The sum of a subtree is the sum of all the node values in that subtree, including its root. Given a binary tree of integers, find the most frequent sum (or sums) of its subtrees. Example For t = { "value": 1, "left": { "value": 2, "left": null, "right": null }, "right": { "value": 3, "left": null, "right": null } } the output should be mostFrequentSum(t) = [2, 3, 6] . Since all the sum values in this tree occur only once, return all of them in ascending order. For t = { "value": -2, "left": { "value": -3, "left": null, "right": null }, "right": { "value": 2, "left": null, "right": null } } the output should

Tree Diameter

Image
https://app.codesignal.com/interview-practice/task/Sby2j4SHqDncwyQjh You got sick because of walking in the woods at night, and have to spend a week at home without going out. Looking out of the window at the nearby woods you're wondering if there is anything you don't yet know about them. Suddenly you see a beautiful and very tall  tree  you haven't seen before. Since you have nothing to do, you decide to examine its structure and draw it in your notebook. You became so exited about this new tree that your temperature went up, so you were forced to stay in bed. You can't see the tree from your bed, but luckily you have it drawn down. The first thing you'd like to find out about is the tree height. Looking at your drawing you suddenly realize that you forgot to mark the tree bottom and you don't know from which vertex you should start counting. Of course a tree height can be calculated as the length of the longest path in it (it is also called  tree diameter ).

Kill i-th bit

https://app.codesignal.com/arcade/code-arcade/corner-of-0s-and-1s/b5z4P2r2CGCtf8HCR In order to stop the Mad Coder evil genius you need to decipher the encrypted message he sent to his minions. The message contains several numbers that, when typed into a supercomputer, will launch a missile into the sky blocking out the sun, and making all the people on Earth grumpy and sad. You figured out that some numbers have a modified single digit in their binary representation. More specifically, in the given number  n  the  k th  bit from the right was initially set to  0 , but its current value might be different. It's now up to you to write a function that will change the  k th  bit of  n  back to  0 . Example For  n = 37  and  k = 3 , the output should be killKthBit(n, k) = 33 . 37 10  = 100 1 01 2  ~> 100 0 01 2  = 33 10 . For  n = 37  and  k = 4 , the output should be killKthBit(n, k) = 37 . The  4 th  bit is  0  already (looks like the Mad Coder forgot to encrypt this number), so t

Financial Crisis

Image
https://app.codesignal.com/arcade/graphs-arcade/kingdom-roads/wkhfpfiT6YynLk2qE Once upon a time, in a kingdom far, far away, there lived a King Byteasar IV. His kingdom in the middle of a financial crisis, Byteasar was struggling to keep the economy out of a recession. Unfortunately there was not much he could do, and after much agonizing he came to the only solution: one of his cities had to be demolished, since keeping communication active between all the cities is way too expensive. It is not yet known if Byteasar chose a city to destroy after careful planning or picked one at random. As a person with a PhD in History and Nobel prize in Computer Science, you can solve this mystery! Archaeologists have recently found a manuscript with information about the roads between the cities, that is now stored in the  roadRegister  matrix. You want to try and remove each city one by one and compare the road registers obtained this way. Thus you'll be able to compare the obtained roads and

Efficient Road Network

Image
https://app.codesignal.com/arcade/graphs-arcade/kingdom-roads/ty4w8WJZ4sZSBNK5Q Once upon a time, in a kingdom far, far away, there lived a King Byteasar III. As a smart and educated ruler, he did everything in his (unlimited) power to make every single system within his kingdom efficient. The king went down in history as a great road builder: during his reign a great number of roads were built, and the road system he created was the most efficient during those dark times. When you started learning about Byteasar's III deeds in your history classes, a creeping doubt crawled into the back of your mind: what if the famous king wasn't so great after all? According to the most recent studies, there were  n  cities in Byteasar's kingdom, connected by two-way  roads . You managed to get information about these  roads  from the university library, so now you can write a function that will determine whether the road system in Byteasar's kingdom was efficient or not. A road syst