Problems - MishiPay Software Engineer Hiring Challenge
Problems - MishiPay Software Engineer Hiring Challenge
Toy shop
There are N shops present on a number line, where each shop is located at
xi coordinate and has toysi number of toys in it.
You are present at point P initially and can move at most k steps.
Whenever you reach a shop, you get all the toys present in that shop.
Task
Determine the maximum toys you can gather after moving at most k steps.
Notes
Example
Assumptions
T=1
N=3
P=0
k=9
x = [-9, -7, 1]
toys = [9, 1, 10]
Approach
Since you start at coordinate 0 and you can only move 9 steps, you are only left with
two good choices. Either you move to the left till -9, or first move to the right till 1
and then reverse the direction to go till -7. You cannot go to -9 in the latter choice as
your steps are exhausted [0 -> 1(1 steps), 1 -> -7 (8 steps)].
If you move to -9 from 0, you collect (1 + 9) = 10 toys.
You move from 0 to 1(1 step) and then to -7(8 steps), you collect (10 + 1) = 11 toys.
Function description
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 1/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
Complete the function getMaxToys provided in the editor. This function takes the
following 5 parameters and returns the required answer:
Input format
Note: This is the input format that you must use to provide custom input (available
above the Compile and Test button).
The first line contains T denoting the number of test cases. T also specifies the
number of times you have to run the getMaxToys function on a different set of
inputs.
For each test case:
The first line contains N denoting the number of shops.
The second line contains P denoting the initial point.
The third line contains k the denoting the maximum number of steps.
The fourth line contains N space-separated values denoting the xi of each
shop.
The fifth line contains N space-separated values denoting the toysi of each
shop.
Output format
For each test case in a new line, print the maximum number of toys.
Constraints
1≤T≤20
1≤N≤105
−106≤xi,P≤106
1≤k≤1012
1≤toysi≤109
Sample input 1
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 2/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
Copy
-5 -1 2 5
10 2 3 4
Sample output 1
Copy
Explanation
Given
T=1
N=4
P=0
k=4
x = [-5, -1, 2, 5]
toys = [10, 2, 3, 4]
Approach
Initially, you start at coordinate 0 and can move at most 4 steps. It can be noticed
that you cannot reach -5 or 5. So the optimal move should be reaching the shops at
-1 and 2.
If you start moving to shop at coordinate 2, you will use 2 steps, then to -1 will take
3 steps making a total of 5 steps, hence you could not be able to take 3 toys at the
shop present on coordinate 2.
If you first started towards shop at -1, it would take 1 step, and then shop at 2 would
take 3 steps making a total of 4 steps, hence you are able to take (2 + 3) = 5 toys.
The following test cases are the actual test cases of this question that may be used to
evaluate your submission.
Sample input 2
Copy
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 3/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
10 9 15
-18 -6 -1 1 7 11 12 16 17 20
77 50 73 86 57 50 6 27 91 76
10 -13 8
46 73 77 32 77 76 14 21 46 16
10 -19 35
-20 -11 -2 1 2 8 9 10 15 16
87 54 81 45 100 26 93 17 24 77
Sample output 2
Copy
307
262
517
64
497
View more
Sample input 3
Copy
10 17 4
-18 -12 -9 -4 0 1 5 9 10 20
73 94 80 92 8 78 45 31 46 8
10 20 39
72 16 22 72 18 86 61 22 85 72
10 -1 38
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 4/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
58 38 67 5 43 93 53 50 78 65
Sample output 3
Copy
526
485
503
584
View more
Note: Your code must be able to print the sample output from the provided sample input. However, your code is
run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the
problem statement.
New Submission
All Submissions
11
12
13
14
15
16
17
18
19
20
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 5/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
distance = sum(map(int,str(abs(P-x[i]))))
if distance > k:
continue
else:
toy_count += toys[i]
for j in range(N):
if i==j:
continue;
if sum(map(int,str(abs(P-x[j])))) > k:
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 6/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
continue
else:
distance += sum(map(int,str(abs(P-x[j]))))
toy_count += toys[j]
if toy_count > max_toy_count:
max_toy_count = toy_count
return max_toy_count
T = int(input())
for _ in range(T):
N = int(input())
P = int(input())
k = int(input())
x = list(map(int, input().split()))
toys = list(map(int, input().split()))
out_ = getMaxToys(N, P, k, x, toys)
print (out_)
26:5 vscode
Next Question
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 7/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 8/9
9/18/21, 2:10 PM Problems - MishiPay Software Engineer Hiring Challenge
https://assessment.hackerearth.com/challenges/hiring/mishipay-software-engineer-hiring-challenge/problems/ 9/9