[go: up one dir, main page]

0% found this document useful (0 votes)
18 views9 pages

Test Algoritmos

Uploaded by

Max Valenzuela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Test Algoritmos

Uploaded by

Max Valenzuela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

TEST BACK 2 SECUNDARIO  mvalenzueladv@gmail.

com

You can view this report online at : https://www.hackerrank.com/x/tests/1574182/candidates/53760851/report

Full Name: Max Henry Valenzuela Del Villar

Email: mvalenzueladv@gmail.com scored in TEST BACK 2


0% SECUNDARIO in 55 min 3 sec
Test Name: TEST BACK 2 SECUNDARIO
on 13 Jul 2023 13:30:44 -05
0/150
Taken On: 13 Jul 2023 13:30:44 -05

Time Taken: 55 min 3 sec/ 60 min

Work Experience: > 5 years

Invited by: Carlos

Invited on: 13 Jul 2023 11:28:09 -05

Skills Score: Problem Solving (Basic) 0/150

Tags Score: Algorithms 0/150

Arrays 0/50

Binary Search 0/50


Data Structures 0/50

Easy 0/150
Interviewer Guidelines 0/50

Loops 0/50

Problem Solving 0/150


Strings 0/50

Theme: E-commerce 0/50


Theme: Finance 0/50

Recruiter/Team Comments:

No Comments.

Question Description Time Taken Score Status

Q1 Project Estimates  Coding 53 min 10 sec 0/ 50 


Q2 Password Creation  Coding 0/ 50 
Q3 Coding Friends  Coding 0/ 50 

QUESTION 1 Project Estimates 



Coding Binary Search Easy Data Structures Algorithms Arrays

Problem Solving Theme: Finance


Not Submitted

QUESTION DESCRIPTION
Score 0

1/9
A number of bids are being taken for a project. Determine the number of distinct pairs of project costs
where their absolute difference is some target value. Two pairs are distinct if they differ in at least one
value.

Example
n=3
projectCosts = [1, 3, 5]
target= 2

There are 2 pairs [1,3], [3,5] that have the target difference target = 2, therefore a value of 2 is returned.

Function Description
Complete the function countPairs in the editor below.

countPairs has the following parameter(s):


int projectCosts[n]: array of integers
int target: the target difference

Returns
int: the number of distinct pairs in projectCosts with an absolute difference of target

Constraints
5 ≤ n ≤ 105
0 < projectCosts[i] ≤ 2 × 109
Each projectCosts[i] is distinct, i.e. unique within projectCosts
1 ≤ target ≤ 109

Input Format for Custom Testing

Input from stdin will be processed as follows and passed to the function.

The first line contains an integer n, the size of the array projectCosts.
The next n lines each contain an element projectCosts[i] where 0 ≤ i < n.
The next line contains the integer target, the target difference.

Sample Case 0

Sample Input 0

STDIN Function
----- --------
5 → projectCosts[] size n = 5
1 → projectCosts = [1, 5, 3, 4, 2]
5
3
4
2
2 → target = 2

Sample Output 0

Explanation 0
Count the number of pairs in projectCosts whose difference is target = 2. The following three pairs meet
the criterion: (1, 3), (5, 3), and (4, 2).

2/9
Sample Case 1

Sample Input 1

STDIN Function
----- --------
10 → projectCosts[] size n = 10
363374326 → projectCosts = [363374326, 364147530, 61825163,
107306571, 128124602, 139946991, 428047635, 491595254, 879792181,
106926279]
364147530
61825163
107306571
128124602
139946991
428047635
491595254
879792181
106926279
1 → target = 1

Sample Output 1

Explanation 1
Count the number of pairs in projectCosts whose difference is target = 1. Because no such pair of
integers exists, return 0.

Sample Case 2

Sample Input 2

STDIN Function
----- --------
6 → projectCosts[] size n = 6
2 → projectCosts = [2, 4, 6, 8, 10, 12]
4
6
8
10
12
2 → target = 2

Sample Output 2

Explanation 2
Count the number of pairs in projectCosts whose difference is target = 2. The following five pairs meet
the criterion: (2, 4), (4, 6), (6, 8), (8, 10), and (10, 12).

CANDIDATE ANSWER

 No answer was submitted for this question. Showing compiled/saved versions.

Language used: Java 8

1
2 class Result {

3/9
3
4 /*
5 * Complete the 'countPairs' function below.
6 *
7 * The function is expected to return an INTEGER.
8 * The function accepts following parameters:
9 * 1. INTEGER_ARRAY projectCosts
10 * 2. INTEGER target
11 */
12
13 public static int countPairs(List<Integer> projectCosts, int target) {
14 // Write your code here
15 for(int projectCost: projectCosts){
16
17 }
18
19
20 }
21
22 }
23

No Comments

QUESTION 2 Password Creation 



Coding Easy Strings Problem Solving Theme: E-commerce

Algorithms
Not Submitted

QUESTION DESCRIPTION
Score 0
A password manager wants to create new passwords using two strings given by the user, then combined
to create a harder-to-guess combination. Given two strings, interleave the characters of the strings to
create a new string. Beginning with an empty string, alternately append a character from string a and from
string b. If one of the strings is exhausted before the other, append the remaining letters from the other
string all at once. The result is the new password.

Example
If a = 'hackerrank' and b = 'mountain', the result is hmaocuknetrariannk.

Function Description
Complete the function newPassword in the editor below.

newPassword has the following parameter(s):


string a: the first string
string b: the second string

Returns:
string: the merged string

Constraints
1 ≤ lengths of a, b ≤ 25000
All characters in a and b are lowercase letters in the range ascii['a'-'z']

Input Format For Custom Testing

Input from stdin will be processed as follows and passed to the function:

4/9
The first line contains the string a.
The second line contains the string b.

Sample Case 0

Sample Input

STDIN Function
----- -----
abc → a = 'abc'
def → b = 'def'

Sample Output

adbecf

Explanation

Alternately taking characters from each string, the merged string is 'adbecf'.

Sample Case 1

Sample Input

STDIN Function
----- -----
cat → a = 'cat'
rabbit → b = 'rabbit'

Sample Output

craatbbit

Explanation

Alternately taking characters from each string, the merged string is 'craatbbit'. After a is exhausted, the
remainder of b is concatenated to get 'craatbbit'.

CANDIDATE ANSWER

 This candidate has not answered this question.

No Comments

QUESTION 3 Coding Friends 



Coding Easy Loops Algorithms Problem Solving Interviewer Guidelines

Not Submitted
QUESTION DESCRIPTION

Sam and Kelly are programming buddies. Kelly resolves to practice more as Sam is ahead initially. They
Score 0
each solve a number of problems daily. Find the minimum number of days for Kelly to have solved more
problems than Sam. If Kelly cannot surpass return -1.

Example
samDaily = 3
kellyDaily = 5
difference = 5

5/9
Initially, Sam has solved difference problems more than Kelly. Each day, they solve samDaily and
kellyDaily problems each.
Day 1: samSolved = difference + samDaily = 5 + 3 = 8
kellySolved = kellyDaily = 5
Day 2: samSolved = 8 + 3 = 11
kellySolved = 5 + 5 = 10
Day 3: samSolved = 11 + 3 = 14
kellySolved = 10 + 5 = 15

Sam is 5 problems ahead of Kelly and they solve 3 and 5 problems a day. Sam will be ahead by only 3
after the first day, 1 after the second, and Kelly will pass Sam on day 3.

Function Description
Complete the function minNum in the editor below.

minNum has the following parameter(s):


samDaily: Number of problems Sam solves in a day
kellyDaily: Number of problems Kelly solves in a day
difference: Number of problems Sam is ahead to begin
Return
int: the minimum number of days needed by Kelly to exceed Sam, or -1 if it is impossible

Constraints
1 ≤ samDaily, kellyDaily ≤ 100
0 ≤ difference ≤ 100

Input Format For Custom Testing

Input from stdin will be processed as follows and passed to the function.

The first line contains an integer samDaily.


The second line contains an integer kellyDaily.
The third line contains an integer ahead.

Sample Case 0

Sample Input 0

STDIN Function
----- --------
3 → samDaily = 3
5 → kellyDaily = 5
1 → difference = 1

Sample Output 0

Explanation 0
Sam is 1 problem ahead of Kelly to begin. After 1 day passes, Kelly will have solved 5 problems while
Sam will have only solved 1 + 3 = 4 problems.

Sample Case 1

Sample Input 1

STDIN Function
----- --------

6/9
4 → samDaily = 4
5 → kellyDaily = 5
1 → difference = 1

Sample Output 1

Explanation 1
Sam is 1 problem ahead of Kelly to begin. After 1 day passes, Kelly will have solved 5 problems while
Sam will have also solved 1 + 4 = 5 problems. On the second day, Kelly will surpass Sam, 5 + 5 > 1 + 4
+ 4.

INTERVIEWER GUIDELINES

Hint 1

What is the minimum extra number of problems Kelly must solve in order to achieve her goal?
Ans - ahead + 1. A 1 is added because Kelly needs to solve more problems than Sam. ahead makes
them equal.

Hint 2

How much ahead is decreasing each day? What are the possible cases when Kelly can never achieve
her goal?
Ans - It is equal to the difference between Kelly and Sam. If this difference is negative or 0, then it is
not possible for Kelly to have solved more problems.

Solution

Concepts Covered: Basic Math, Loops, Binary Search, Problem Solving


The problem can be solved using a simple brute force search or a binary search. These test the
candidate's skills to implement loops and the binary search algorithm. Alternatively, the candidate can
come up with a formula using some basic maths to solve the problem.

Optimal Solution:
Observe that the number of days will be (ahead + 1)/difference covered each day by Kelly. Note that
we need to handle the cases when the difference is 0.
Time Complexity - O(1)

from math import ceil


def minNum(samDaily, kellyDaily, difference):
dif = kellyDaily - samDaily
# if Kelly solves the same or fewer, Kelly can't catch up
if dif<=0:
return -1
# otherwise, calculate days (+1 enforces strictly greater)
days_required = ceil((difference+1)/dif)
return days_required

Brute Force Approach:


We can iterate on the number of days. Each day, we decrease the variable ahead by the difference
between the number of problems solved by Kelly and Sam. If the difference is negative or zero, it is
not possible. Hence return -1.
Time Complexity - O(Number of days)

Binary Search Solution:


We can binary search on the number of days. As the number of days increases, the difference
between total problems solved also increases.
Time Complexity - O(log(Number of days))

7/9
Error Handling:
1. The edge case for non-positive difference needs to be handled separately.
2. It is important to observe that in order to solve more problems, Kelly must solve difference + 1 more
problems. Solving only difference just makes them equal.
3. Finally, it is very important to get the ceiling of the division and not simple division or floor.

Complexity Analysis

Time Complexity - O(1) - We have derived a formula for the calculation of number of days. Hence the
complexity is O(1).
Space Complexity - O(1) - Constant extra space is required.

Follow up Question

Suppose on an ith day, Kelly solves i more problems than Sam. What is the minimum number of days
now?

We can see that after i days, the number of problems more problems solved by Kelly would be i*(i+1)/2.
We need to find the minimum i, such that i*(i+1)/2 is greater than ahead.
Solution 1 -
We can binary search over the number of days. The complexity of this would be log(Number of days).
Space complexity would be O(1).

def minNum(ahead):
lo = 1, hi = sqrt(ahead)
while hi - lo > 1:
mid = (hi + lo)//2
lead = mid * (mid + 1) // 2
if lead > ahead:
hi = mid
else:
lo = mid
return hi

Follow up Question

In the previous problem, how can we optimize the time complexity?

We can reduce the time to O(1). We see that we need an i such that i * (i+1) / 2 >= K, where K is the
number of extra problems that need to be solved. Solving this equation a bit, we get i2 + i - 2 * K >= 0.
We can use the following formula to get i.

We can take the ceil of the value to get the answer.


Psuedo Code -

from math import sqrt, ceil


def minNum(ahead):
K = ahead + 1
num = -1 + sqrt(1 + 8 * K)
den = 2
ans = ceil(num / den)
return ans

CANDIDATE ANSWER

8/9
 This candidate has not answered this question.

No Comments

PDF generated at: 13 Jul 2023 19:32:44 UTC

9/9

You might also like