0 like 0 dislike
4,117 views
| 4,117 views

0 like 0 dislike

A couple of online assesment questions and solutions that are being asked in Google OA (US Based Position) - HackerRank

Question 1

One fragment of a given integer N can be selected and its digits reversed (replaced with a right to left version of themselves). What is the maximum number that can beobtained this way from integer N?
Write a function:

`class Solution { public int solution(int N); }`

that, given an integer 1 <= N <= 1,000,000,000, returns the greatest integer that can be created by reversing a subset of its digits.

Examples:
• Given N = 5340, [he answer is 5430. Fragment "34" can be reversed to "43".
• Given N = 2043, the answer is 4023. Fragment "204" can be reversed to "402"
• Given N = 620, the answer is 620. There is no need to reverse any fragment.

Assumptions:
• 1 <= N <= 1.000.000.000.

Solution 1

``````import sys
def solution(N):

N = str(N)
r, l = 0, 0

for i in range(1, len (N)) :
if int(N[il) > int (N[i - 1]):
r = i

for i in range (0, len(N)) :
if int (N[i]) < int (N[r]) :
l = i
break

rev = N[l : r + 1]
rev = rev[::-1]
return int(N[:l] + rev + N[r+l:])
``````

Question 2:

Given a list of numbers include the number in the list, and if that number is present in the list include the reversed number in the list. Return the length of the list after adding the elements to the list.

Solution 2

``````import sys
def solution(A):
result = set()
for nums in A:
nums = (2 - len(str(nums))) * "0" + str(nums)
if nums in result:
else:
return len(result)
``````

Question 3

We are given a string text of length N consisting of the letters a', 'b' or'c'. We can insert any of those letters before or after any letter in the string. The goal is to insert letters into text so that it will follow the pattern "abcabca.…..i.e. it should start with a letter a'. letter 'a' should be followed by "b', letter b' should be followed by 'c', and letter c' by 'a'. The string may end with any of those three letters. What is the minimum number of letters we need toinsert into text?

Write a function:
`int solution(String text);`

that, given a string text of length N, returns the minimum number of insertions needed to make text follow the described pattern.

Examples:

1. For text = "aabcc* we need to insert letters 'b' and c' between the pair of letters "a, and then insert letters 'a' and 'b' between the two letters C'. This way we obtain the string "abcabcabc* and the function should return 4.
2. For text = abcabcabca", we do not need to insert any letters. The string already follows the required pattern, so the function should return O. Note that text does not need to end with letter c.
3. For text = "bcaaa*, letter a' should be inserted at the beginning of text, and then letters b' and c should be inserted between the two pairs of letters a. This way we obtain the string "abcabcabca" and the function should return 5.

Assume that:
• N is an integer within the range (1..100]:
• string text consists only of the following characters: "a", "b" and/or c"

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Solution 3

``````input_text = "aabcc"
result = 0
hmap = {"aa": 2, "ba": 1, "bb": 2, "cb": 1, "ac": 1, "cc": 2, "b": 1, "c": 2}

for i in range(len(input_text)):
if i == 0:
result += hmap.get(input_text[i], 0)
else:
result += hmap.get(input_text[i-1]+input_text[i], 0)
return result
``````

Question 4

You are given an array segments consisting of N integers denoting the lengths of several segments. Your task is to find among them four segments from which a rectangle can be constructed. What is the minimum absolute difference between the side lengths of the constructed rectangle?

Write a function:
`int solution(int[] segments);`

that, given an array segments, returns the minimum absolute difference between the side lenaths of the constructed rectangle or -1 if no rectangle can be constructed.

Examples:

1. For segments = [2, 2, 2, 2, 2], we can construct only a 2 x2 rectangle out of the given segments. The function should return 0.
2. For segments = [911, 1, 3, 1000, 1000, 2, 2; 999,
1000, 911], we can construct three rectangles: 2 x 911. 2x 1000, and 911 x 1000. Out of those three
possibilities, the best one is 911 x 1000. The function should return 89.
3. For segments = [4, 1, 1, 1, 3], we cannot construct any rectangle out of the given segments. The
function should return -1.
4. For segments = [1, 1, 11, we cannot construct any rectangle out of the given segments. The function should return -1.

Assume that:
• N is an integer within the range [1.30];
• each element of array segments is an integer within the range [1..,1000]. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Solution 4

``````
def solution(segments):
if len(segments) < 4:
return -1
import collections
hmap = collections.Counter(segments)
result = []
for k, v in hmap.items():
result.extend([k] * (v // 2))

if len(result) < 2:
return -1
result = sorted(result)
smallest = float("+inf")
for i in range(1, len(result)):
smallest = min(smallest, result[i] - result[i - 1])
return smallest``````
by Expert (107,750 points)