Question: PayPal | 23rd September | Online Assessments | Reshelving Packets | Similar Order | Minimum User Log
1
Entering edit mode

Question 1

Click here to Practice

Reshelving Packets

Range of shelves from 1 to 99 inclusive Given list of integers, reshelve them to the minimum value possible and return the array in the same order though.

Example 1

Input

[1, 12, 4, 12] - List of shelves

Output

[1, 3, 2, 3]

Explanation - Starting shelf is 1, so that remains same, next lowest available value is 2, which can be assigned to 4 and the next available shelf is 3 that can be assigned to 12.

Example 2

Input

[1, 2, 6, 5]

Ouput

[1, 2, 4, 3]

Question 2

Click here to Practice

Similar Order

Given List of String represnting each order, from 2 customers, and each character in the order representing item, find if orders were similar. Conditions for being similar, should be same in length and number of distinct characters should not be more than 3.

Example -

Input

x : ["nnbnnb", "ammxdf"]

y : ["bbncbb", "aecxdf"]

Output

["YES", "NO"]

Explanation

x[0] is similar to y[0] as both same in length and

x[0] has 4 "n", y[0] has 1 "n", so delta is 3,

x[0] has 2 "b", y[0] has 2 "b", so delta is 2,

x[0] has 0 "c", y[0] has 1 "c", so delta is 1

Question 3

Click here to Practice

Minimum User Log

Given list of strings which are logs and an integer representing maximum time, determine which user has activity time of less than or equal to the maximum time. Sort and return those list of users.[Sorting was based on user number or their activity time not specified]

Input

logs : ["12 10 sign-in","5 10 sign-in", "5 100 sign-out", "12 20 sign-out", "8 20 sign-in", "8 50 sign-out"]

maximumTime : 30

Output

[8, 12]

Explanation

User 12 - logged in time period - (signout time - signin time) - 20 - 10 = 10 < 30

User 5 - logged in time period - (signout time - signin time) - 100 - 10 = 90 > 30

User 8 - logged in time period - (signout time - signin time) - 50 - 20 = 30 == 30

1
Entering edit mode

Question1

Overview

  • We are given a list of packets in form of integer values we have to assign each packet a number based on the sorted values of the packets but keep in mind we can't change the position of any packet.
    • If any number is repeating in the list we are going to assign the same number that was assigned to its previous position.
    • It's also given that the range of shelves is from 1 to 99 only

Solution/Explanation

  • In order to assign each shelf a value accordingly we first need to sort the array into a dummy array as we are going to pick first the lowest value and then keep on increasing.
  • Let's say to_be_assigned=1 that is we will be going to assign the first smallest value as 1 then keep increasing to_be_assigned variable by 1. But here is a catch if two similar elements appear in an array then how we are going to handle that case? So the answer is quite simple we are going to make a map like map&lt;int, int&gt;M and for each element in that dummy array we are going to iterate it and check if the cur element is already present in the map or not if(!M[dummy[i]]) if the current element is not present in the map we are going to assign M[dummy[i]]=to_be_assigned; to_be_assigned++ it to our map and increase our to_be_assigned value.
  • As the last step we are going to create a result array of a size similar to the original array and iterate over the original array and assigned the value to the result array like: Result[i]=M[orignal[i]]
  • In the last simply print the resultant array.
  • Time Complexity : O(NlogN)
ADD COMMENTlink 2.2 years ago Akshay Sharma 990
1
Entering edit mode

Question 2

Overview

  • Given us the list of two customers that contains some string values in it that the order they placed in every character of that string represents the items and orders are considered to be the same if the difference of each item between two customers is less than equal to 3 and the size of each order they both place is equal else the order are considered different.
  • For each item in the list we need to tell if they have the same order or not. If they placed the same order then print YES otherwise print NO.

Solution

  • We are given two lists for two different persons so the first thing that we are going to do is to create two different vectors of string for person 1 and person second.
    • After that, we need to push all the orders in that vector of string for both the person separately keep in mind all orders are separated by spaces.
    • After that, the main intuition starts we are going to iterate over all the orders of any of the persons and check if the length of that order is equal to the corresponding order of the second person or not. If the length is not the same we simply print that particular order is not the same else we are going to maintain two vectors of size 26 named freq1 and freq2 for both the orders and iterate over the current order and store the number of times a character occurred for both of the person orders.
    • Lastly, we iterate from 0 to 26 and check if the difference between each character is greater than 3 or not if and is yes then we break our loop and print "NO" else print "YES"
    • Don't forget to reset the freq1 and freq2 vector after each iteration of order.
ADD COMMENTlink 2.2 years ago Akshay Sharma 990
1
Entering edit mode

Question 3

Since we are getting strings, we can split the string by space and that way for each entry we will get three things : user_id, time, sign-in/out. For each user we will have one or more sign in times and their corresponding sign out times. we can maintain a hash_map of user ids and for every sign in and corresponding sign out we can subtract and add the time to the value of that user id in the hash map. If there are multiple sign in and sign out times for a given user, then we will have the sum of all the activities as the total activity time of that user in the hash map. Now the next problem is to calculate the sign in and sign out time. Like we were maintaining a hash map for the total activity for every user, similarly we can maintain one for the sign in time of every user, and every time we come across a sign out time, we can subtract the corresponding sign in time from the has map. For every sign in time for user [u,t1,sign_in] :

hash_map_time[u] = t

For every sign out time for user [u,t2,sign_out]:

hash_map_activity[u] += t2 - hash_map_time[u]

Note that we are maintaining 2 hash maps as mentioned earlier, hash_map_activity stores the total activity time and hash_map_time stores the latest sign in time.

after we have all the activity time, we can put all the user_id which have an activity time less than the maximum time in an array, sort the array and print the user id's in a sorted format.

ADD COMMENTlink 2.2 years ago Manas 330

Login before adding your answer.

Similar Posts
Loading Similar Posts