Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,007 views
in Online Assessments by Expert (46,090 points) | 1,007 views

3 Answers

0 like 0 dislike
Best answer
  1. get the sum of an array of integers and return [-1,0,1] is the sum is negative, 0, positive.
    [1,-2,-3,5] = 30 return 1
    [1,-2,3,5] = -30 return -1
    [1,-2,3,0] = 0 return 0
    sum = nums[0]
    sum *= nums[1..n]
    return (sum > 0) ? 1 : (sum < 0) ? -1 : 0;
  2. https://stackoverflow.com/questions/23113268/monotonic-pair-codility
    optimize the solution.
    given an array of int [2,6,2,6,1] return the largest difference in index of a number's reoccurence in the array.
A[0] = 2  -> 0,2  -> 0-0, 2-0 = 2
A[1] = 6  -> 1,3,6 -> 1-1, 3-1, 6 - 1 = 5
A[2] = 2  -> 0,2 
A[3] = 6  -> 1,3,6
A[4] = 1  -> 4 -> 4-4 = 0
A[5] = 6  -> 1,3,6 

 

return 5.
you are given the answer in O(n^2) time that use 2 for loops and an if(nums[i] == nums[j]) result = Math.max(result, j - i);
we only care about the largest index of a number in the array.
I store the numbers in a map and update the index as value
iterate through the nums array once and lookup the map.
result = Math.max(result, map.get(nums[i] - i))
This should be O(n) time and O(n) space

 

  1. given an integer, remove a number 5 from it and return the largest value.
    15985 -> 1985 or 1598, return 1958
    -15958 -> -1958 or -1598, return -1598
    convert number to stringbuilder
    if(n > 0)
    get the first index of 5 and delete from stringbuilder
    else if(n < 0)
    get the last index of 5 and delete from stringbuilder
by Expert (46,090 points)
0 like 0 dislike
In question 3, it is not optimal to remove the first occurrence of 5 in positive number.
For eg, n = 15159
Your answer: 1159
Expected answer: 1519
by Expert (46,090 points)
0 like 0 dislike

Hi the first problem is related to this LC problem.
https://leetcode.com/problems/sign-of-the-product-of-an-array/

by Expert (46,090 points)