Professional Documents
Culture Documents
PLACEMENT
ASSESSMENT TEST - I
(Hosted on Hackerrank and Unstop)
Date: 21st January 2024
SOLUTIONS
1
Coding Section
1. Buffet 1
Explanation : Insert all elements of the array in a priority queue (let’s call this pq), pop the
top element (let’s call this el) in pq, add it to the ans variable, insert el/2 (integer* division) in
pq.
* - Note: The quantity of food can be 10¹⁰, which will not fit in int, you should use long/long
long to avoid overflow
C++:
#include <bits/stdc++.h>
#define nl cout<<"\n"
ll ans=0;
rep(k){
ll x=ms.top();
ms.pop();
ans+=x;
ms.push(x/2);
2
}
cout<<ans;nl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++)
{
solve();
}
}
Java:
import java.util.PriorityQueue;
import java.util.Scanner;
long ans = 0;
while (k > 0) {
k--;
// Pop the buffet with the maximum amount of food
3
long maxFood = maxHeap.poll();
ans += maxFood;
long rem = (long) maxFood / 2;
maxHeap.offer(rem);
}
return ans;
}
Python:
import heapq
def max_food_eaten(N, k, buffet_amounts):
# Convert buffet amounts to max heap, use -amount as heapq.heapify creates a min heap by default
max_heap = [-amount for amount in buffet_amounts]
heapq.heapify(max_heap)
ans = 0
while k > 0:
k -= 1
4
# Pop the buffet with the maximum amount of food
max_food = -heapq.heappop(max_heap)
ans += max_food
rem = (max_food//2)
heapq.heappush(max_heap,-1*rem)
return ans
t=int(input())
for _ in range (t):
n, k = map(int,input().split())
b = list(map(int,input().split()))
ans = max_food_eaten(n,k,b)
print(ans)
2. Pairland
Explanation: Use the concept of 0/1 knapsack dp. The magical number becomes the
capacity of the knapsack. dp[i][j] represents whether you can obtain the sum ‘j’ by using i
coins.
Let a and b be the values of the ith coin. You can obtain a sum of j using the ith coin, if you
can obtain the sum of (j-a) or (j-b) using (i-1)th coin.
C++ :
#include <bits/stdc++.h>
#include <iostream>
using ll=int;
#define all(x) (x).begin(), (x).end()
#define pb push_back
using namespace std;
void solve()
{
ll n,s;
5
cin>>n>>s;
vector<pair<ll,ll>>v(n);
for(int i=0;i<n;i++){
cin>>v[i].first>>v[i].second;
}
vector<vector<int>>dp(n,vector<int>(s+1,0));
dp[0][v[0].first]=1;
dp[0][v[0].second]=1;
for(int i=1;i<n;i++){
for(ll j=0;j<=s;j++){
int pick_a=0;
int pick_b=0;
if(j-v[i].first>=0){
pick_a=dp[i-1][j-v[i].first];
}
if(j-v[i].second>=0){
pick_b=dp[i-1][j-v[i].second];
}
dp[i][j]=max(pick_a,pick_b);
}
}
if(dp[n-1][s]>0){
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++)
{
6
solve();
}
}
Java :
import java.util.Scanner;
if (a[0] <= k) {
dp[0][a[0]] = 1;
}
if (b[0] <= k) {
dp[0][b[0]] = 1;
}
7
if (a[i] <= j) {
l = dp[i - 1][j - a[i]];
}
if (b[i] <= j) {
r = dp[i - 1][j - b[i]];
}
dp[i][j] = l | r;
}
}
Python:
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = [0]*n
b = [0]*n
for i in range(n):
a[i], b[i] = map(int, input().split())
dp = [[0 for _ in range(k + 1)] for _ in range(n)]
if a[0] <= k:
dp[0][a[0]] = 1
if b[0] <= k:
dp[0][b[0]] = 1
for i in range(1, n):
for j in range(1, k+1):
l, r = 0, 0
if a[i] <= j:
l = dp[i-1][j-a[i]]
if b[i] <= j:
r = dp[i-1][j-b[i]]
8
dp[i][j] = l or r
print("YES" if dp[n-1][k] else "NO")
Aptitude Section
1. What is the angle between hour-hand and minute-hand when the time is 10:35pm?
A) 106.5°
B) 107.5°
C) 108.5°
D) 109.5°
Answer: C) 107.5°
Solution:
To find the angle between the hour and minute hands of a clock at a specific time, you can
use the following formula:
Where H is the number of hours on the clock and M is the number of minutes on the clock.
For 10:35pm,
Angle = | 30(10) - (11/2)35 |
= | 300 - 192.5 |
= 107.5°
2. Three persons A, B and C shoot to hit a target. If in trials, A hits the target 4 times in
5 shots, B hits 3 times in 4 shots and C hits 2 times in 3 trials. find the probability that
exactly two persons hit the target
A) 13/30
B) 19/30
C) 5/6
D) 7/15
Answer: A) 13/30
9
Solution:
P(A) = 4/5 P(A’) = 1/5
P(B) = 3/4 P(B’) = 1/4
P(C) = 2/3 P(C’) = 1/3
Answer: A) 1998
Solution:
Initial Series:
0 14 52 126 248 …
10
= 1998
4. Two trains of length 80 m and 120 m are moving towards each other with speeds
50 m/s and 70 m/s. What is the time they take to cross each other?
A) 0.33 s
B) 10 s
C) 1.67 s
D) 2 s
Answer: C) 1.67 s
Solution:
To find the time it takes for the two trains to cross each other, we can use the relative
speed of the trains. When two objects are moving towards each other, their relative speed
is the sum of their individual speeds.
Let's denote:
v₁ as the speed of the first train (50 m/s),
v₂ as the speed of the second train (70 m/s),
l₁ as the length of the first train (80 m),
l₂ as the length of the second train (120 m).
11
5. An article is listed at price of Rs. 75000. On day 1, the market value of the article
decreases by some percentage. On day 2, the market value of the article increases
by the same percentage of the value at the end of day 1. The value of the article at the
end of day 2 is Rs. 74520. What was the percentage increase on day 2?
A) 0.64%
B) 8%
C) 1%
D) 10%
Answer: B) 8%
Let's denote the original price of the article as P, the percentage decrease on day 1 as x,
and hence the percentage increase on day 2 also becomes x.
Now, we know P = Rs. 75000 and value at the end of day 2 = Rs. 74520
Therefore,
75420 = 75000(1 - x²/10000)
74520 / 75000 = (1 - x²/10000)
0.9936 = 1 - x²/10000
x²/10000 = 1 - 0.9936
x²/10000 = 0.0064
x² = 64
x=8
12
6. If p and q are two whole numbers such that mⁿ = 2197. What is the value of
(m-3)ⁿ-¹?
A) 10
B) 1000
C) 10000
D) 100
Answer: D) 100
Solution:
2197 = 13³
Since 13 and 3 are prime numbers, there is no other permutation in which 2197 can be
represented. Hence, m = 13 and n = 3. Therefore,
7. Samantha told her office colleague Monika that when Monika was born, Samantha
was as old as Monika’s present age. If Samantha’s current age is 36, how old was
Monika 4 years ago?
A) 14
B) 12
C) 18
D) 21
Answer: A) 14
Solution:
Let’s assume Monika was born x years ago. That means, x years ago, Samantha’s age was
also x, thereby meaning that Samantha is x + x = 2x years old currently.
2x = 36
x = 36/2
x = 18 years
13
8. Find out that diagram which portrays the best relation between Tourists, Planes
and Trains?
A)
B)
C)
D)
Answer: D)
Solution:
Planes and Trains will be disjoint sets and since Tourists can travel in both plane and trains
but not both at the same time, the correct answer is D.
9. From the options provided, which day is not a century’s last day?
A) Tuesday
B) Monday
C) Wednesday
D) None of the above
Answer: A) Tuesday
Solution:
1 year has 365 days (365/7=1 odd day / Year )for leap year 366 days (365/7=2 odd /leap
year)
14
100 years contain 5 odd days.
Last day of 1st century is Friday.
10. Dev is three times as good as Sahil. If they finish a task together in 15 days,
calculate the days taken by Dev alone to complete the work.
A) 15
B) 30
C) 20
D) 17
Answer: C) 20
Solution:
Let us assume that Dev takes x days to finish a task. This means that Sahil takes 3x days to
finish the task. If they both work on that same task together they will take:
= ((1 / x) + (1 / 3x))⁻¹
= ( 4 / 3x )⁻¹
= 3x / 4 days
3x / 4 = 15
x = 20 days
15
Computer Fundamentals Section
Answer: C) TRUNCATE
Solution:
The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.
Answer: D) contains a copy of the designated memory location specified by the MAR
after a "read" or the new contents of the memory prior to a "write".
Solution:
The Memory Buffer Register (MBR) is a register in a computer's CPU (Central Processing
Unit) that holds the data that is fetched from memory or the data that is to be written to
memory. It contains the actual data that is read from or written to the memory location
specified by the Memory Address Register (MAR). So, option D accurately describes the
function of the Memory Buffer Register.
16
3. "State the big O of the following fragment:
for(i= 0; i < n; i++){
for(j = 1; j < n; j = j*2){
cout << i << " ";
}
}
A) O(n²)
B) O(n log n)
C) O(2ⁿ)
D) O(n)
Answer: D) Cardinality
Solution:
In an ERD, cardinality defines the numerical relationships between entities. It specifies how
many instances of one entity are related to the number of instances of another entity.
Multiplicity and cardinality are often used interchangeably in this context to describe the
same concept. They help define the nature of the relationships between entities, such as
one-to-one, one-to-many, or many-to-many.
17
5. In C, if you pass an array as an argument to a function, what actually gets passed?
A) Value of elements in array
B) First element of the array
C) Base address of the array
D) Address of the last element of array
Answer: B) i, iv
Solution:
The correct statements about an array are:
18
7. Which of the following is true about thread and process startup speed?
A) The startup of a thread is faster than a process.
B) The process startup is faster as it is directly controlled by the OS.
C) They will be equal.
D) Depends on the OS that is used. Faster on Windows98 slower on NT.
19
9. What is the output of the following code?
#include
void main()
{
int s=0;
while(s++<10)
{
if(s<4 && s<9)
continue;
printf("\n%d\t",s);
}
}
A) 1 2 3 4 5 6 7 8 9
B) 1 2 3 10
C) 4 5 6 7 8 9 10
D) 4 5 6 7 8 9
Answer: C) 4 5 6 7 8 9 10
10. Which of the following types of class allows only one object of it to be created?
A) Virtual Class
B) Abstract Class
C) Singleton Class
D) Friend Class
20