CategoryCoding Challenge

HackerRank ‘Palindrome Index’ Solution

H

Short Problem Definition: You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution. Link Palindrome Index Complexity: time complexity is O(N) space complexity is O(N) Execution: The solution seems n^2 but isPalindrome is executed only once. Due to the problem...

HackerRank ‘Simple Text Editor’ Solution

H

Short Problem Definition: In this problem, your task is to implement a simple text editor. Initially, a file contains an empty string S. Your task is to perform Q operations consisting of the following 4 types: append(W) – Appends the string W at the end of S. erase(k) – Erase the last k character of S. get(k) – Returns the kth character of S. undo() – Undo the last not previously undone...

HackerRank ‘Pangrams’ Solution

H

Short Problem Definition: Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.) Link Pangrams Complexity: time complexity is O(N) space complexity is O(N) Execution:...

HackerRank ‘Sock Merchant’ Solution

H

Short Problem Definition: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. Link Sock Merchant Complexity: time complexity is O(N) space complexity is O(N) Execution: Group the same color together. Look out for the...

HackerRank ‘Picking Numbers’ Solution

H

Short Problem Definition: Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1. For example, if your array is a = [1, 1, 2, 2, 4, 4, 5, 5, 5], you can create two subarrays meeting the criterion:  [1 ,1 ,2 ,2] and [4, 4, 5, 5, 5]. The...

HackerRank ‘Song of Pi’ Solution

H

Short Problem Definition: That’s the value of pi! (Ignoring the floating point) A song is a pi song if the length of its words represent the value of pi. Link Song of Pi Complexity: time complexity is O(N*T) space complexity is O(N) Execution:   This problem is straight forward. Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/usr/bin/py   PI = map(int...

HackerRank ‘Plus Minus’ Solution

H

Short Problem Definition: You’re given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places. Link Plus Minus Complexity: time complexity is O(N) space complexity is O(1) Execution: Count the values. Do not forget to force floats instead of...

HackerRank ‘Poisonous Plants’ Solution

H

Short Problem Definition: There are NN plants in a garden. Each of these plants has been added with some amount of pesticide. After each day, if any plant has more pesticide than the plant at its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each plant. Print the number of days after which no plant dies, i.e. the time after which there are no...

HackerRank ‘Repeated String’ Solution

H

Short Problem Definition: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letters a in the first n letters of Lilah’s infinite string. For example, if the string s = ‘abcac’ and n = 10, the substring we consider is abcacabcac, the first 10 characters of her infinite string. There...

HackerRank ‘Save the Prisoner!’ Solution

H

Short Problem Definition: A jail has N prisoners, and each prisoner has a unique id number,S , ranging from 1 to N. There are M sweets that must be distributed to the prisoners. But wait—there’s a catch—the very last sweet S is poisoned! Can you find and print the ID number of the last prisoner to receive a sweet so he can be warned? Link Save the Prisoner! Complexity: time complexity is O(1)...

Archives

Categories