HackWithInfy

HackWithInfy 2019

Question 1 - Slowest Key Press

Problem Statement

Alex wants to be a faster typist and is taking a typing test to find out which key takes the longest time to press.

Given the results of the test, determine which key takes the longest to press.

For example, given keyTimes =[[0, 2], [1, 5], [0, 9], [2, 15]]. Interpret each keyTimes[i][0] as an encoded character in the range ascii[a-z] where a = 0, b = 1,...z = 25.

The second element, represents the time the key is pressed since the start of the test.

In the example, keys pressed, in order are abac at times 2, 5, 9, 15. From the start time, it took 2 - 0 = 2 to press the first key, 5 - 2 = 3 to press the second, and so on.

The longest time it took to press a key was key 2, or 'c', at 15 - 9 = 6.

Function Description

Complete the function slowestKey in the editor below. The function must return a character, the slowest key that Alex presses.

slowestKey has the following parameter(s):

keyTimes[keyTimes[O],... keyTimes[n-1]]: an array of two integers: The character Alex pressed (encoded) and the time at which it was pressed

Constraints

• 1 <= n <= 10^5

• 0 <= keyTimes[i][0] <= 25 (0 <= i <= n)

• 1 <= keyTimes[i][1] <= 10^8 (0 <= i <= n)

• It is guaranteed that there will only be one key with the worst time. Input Format For Custom Testing

Sample Input
3
2
0 2
1 3
0 7


Sample Output
a

Explanation:
The time taken to press 'a' in the worst case is 7 - 3 = 4. To press 'b' the worst case is 3 - 2 = 1. Alex performed worst on 'a'.

Question 2 - Rock Paper Scissor

Problem Statement

In a Rock Paper Scissor tournament, the contestants stand in a straight line. Each pair of consecutive players compete in a round. If there is an odd number of players, the last one in the line qualifies for the next round without playing.

For each game, each player indicates either a rock, paper or scissors denoted 'R', 'P' or 'S' respectively. Outcomes are as follows:

• paper beats rock, P > R

• scissors beat paper, S > P

• rock beats scissors, R > S

After each round, the winners remain and the losers are out of the competition. In case of a tie, both players lose. A player would like to win the competition and has one advantage: the knowledge that each other player uses only one type of hand formation in all the rounds of the tournament. Determine how many times the player will have to change the hand formation in order to win the competition.

For example, the number of players, n = 3, and the player of interest, POI, is standing at position given by a = 2 (0-based index). The hand formations used by other players are given by formations = "PS" with length n - 1 = 2. In the first round, scissors (S) beats paper (P). POI must then beat the winner of round one who always chooses scissors (S). POI will choose rock (R) and win the tournament after having chosen only one hand formation, hence the answer is 0.

Function Description

Complete the function handFormationChange in the editor below. The function must return an integer, the number of times the POI needs to change hand formation.

handFormationChange has the following parameter(s): n: an integer, the number of players in the tournament a: an inteqer, the POI's position in the line, 0-indexed

Constraints
• 2 <= n <= 10^5

• 0 <= a <= n

• formations[i] belongs to {'R', 'S', 'P'} (0 <= i < n-1)

Sample Input
4
1
PRS

Sample Output
1

Explanation:
In the first round, POI is at position 1 and must beat paper at position O by choosing S, scissors. For the second pair, rock beats scissors. Now the POI must choose paper to beat rock in round two. It takes one change of formation to win the tournament.

Question 3 - Dangerous Script

Problem Statement

If you've ever looked at an assembly language dump of an executable file, you know that commands come in the form of hexadecimal digits that are grouped by the processor into instructions. It is important that the parsing be done correctly or code will not be executed as expected. Wrong parsing is the basis for Return Oriented Programming, a method of causing mahem.

You have developed a program in a new scripting language. Of course, it requires accurate parsing in order to perform as expeced, and it is very cryptic. You want to determine how many valid commands can be made out of your lines of script. To do this, you count all of the substrings that make up a valid command. Each of the valid commands will be in the following format:

• The first letter is a lowercase English letter.

• Next, it contains a sequence of zero or more of the following characters: lowercase English letters, digits, and colons.

• Next, it contains a forward slash '/'.

• Next, it contains a sequence of one or more of the following characters: lowercase English letters and digits.

• Next, it contains a backward slash '\'.

• Next, it contains a sequence of one or more lowercase English letters.

Given some string, s, we define the following: • s[i..j] is a substring consisting of all the characters in the inclusive range between index i and index j.

• Two substrings, s[i[1]..j[1]] and s[i[2]..j[2]] are said to be distinct if either i[1] != i[2] or j[1] != j[2].

For example, your command line is abc:/b1c\xy.

Valid command substrings are:

abc:/b1c\xy

bc:/b1c\xy

c:/b1c\xy

abc:/b1c\x

bc:/b1c\x

c:/b1c\x

There are six valid commands that may be parsed from that one command string.

Function Description
Complete the function commandCount in the editor below. The function must return an array of integers, each representing the number of valid command strings in commands[i].

commandCount has the following parameter(s): commands[commands[0],...commands[n-l]]: an array of strings

Constraints
• 1 <= n <= 50

• Each character of commands[i] belongs to [a-z, 0-9, /, \, :]

• Each |commands[i]| <= 5 * 10^5.

Sample Input
6

w\\//a/b
w\\//a\b
w\\/a\b
w:://a\b
w::/a\b
w:/a\bc::/12\xyz


Sample Output

0
0
0
0
1
8

Explanation:
Let's call our return array ret.

We fill ret as follows:

• commands[0] = "w\//a/b" has no valid command substring, so ret[0] = 0.

• commands[1] = "w\//a\b" has no valid command substring, so ret[1] = 0.

• commands[2] = "w\/a\b" has no valid command substring, so ret[2] = 0.

• commands[3] = "w:://a\b" has no valid command substring, so ret[3] = 0.

• commands[3] = "w::/a\b" has one valid command substring, commands[O..6] = "w::/a\b" so ret[4] = 1

• commands[5] = "w:/a\bc::/12\xyz" has the following eight valid command substrings:

  1. commands[0..5] = w:/a\b
  2. commands[O..6] = w:/a\bc
  3. commands[5..13] = bc::/12\x
  4. commands[5..14] = bc::/12\xy
  5. commands[5..15] = bc::/12\xyz
  6. commands[6..13] = c::/12\x
  7. commands[6..14] = c::/12\xy
  8. commands[6..15] = c::/12\xyz

This means ret[5] = 8

We then return ret = [0, 0, 0, 0, 1, 8].

Comments