You are on page 1of 20

Bharatiya Vidya Bhavan’s

SARDAR PATEL INSTITUTE OF TECHNOLOGY


(Autonomous Institute Affiliated to the University of Mumbai)

PLACEMENT
ASSESSMENT TEST - I
(Hosted on Hackerrank and Unstop)
Date: 21st January 2024

SOLUTIONS

1
Coding Section
1. Buffet 1

Concept: Priority Queues / Heaps

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"

using ll = long long;


#define rep(n) for (ll i = 0; i < n; i++)
using namespace std;
void solve()
{
ll n,k;
cin>>n>>k;
vector<ll>v(n);
priority_queue<ll>ms;
for(ll i=0;i<n;i++){
cin>>v[i];
ms.push(v[i]);
}

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;

public class MaxFoodEaten {

public static long maxFoodEaten(int N, int k, long[] buffetAmounts) {


// Convert buffet amounts to max heap
PriorityQueue<Long> maxHeap = new PriorityQueue<>((a, b) -> Long.compare(b, a));
for (long amount : buffetAmounts) {
maxHeap.offer(amount);
}

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;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();

for (int i = 0; i < t; i++) {


int n = scanner.nextInt();
int k = scanner.nextInt();
long[] b = new long[n];
for (int j = 0; j < n; j++) {
b[j] = scanner.nextLong();
}
long ans = maxFoodEaten(n, k, b);
System.out.println(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

Concept : 2D Dynamic Programming

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();

for (int testCase = 0; testCase < t; testCase++) {


int n = scanner.nextInt();
int k = scanner.nextInt();
int[] a = new int[n];
int[] b = new int[n];

for (int i = 0; i < n; i++) {


a[i] = scanner.nextInt();
b[i] = scanner.nextInt();
}

int[][] dp = new int[n][k + 1];

if (a[0] <= k) {
dp[0][a[0]] = 1;
}
if (b[0] <= k) {
dp[0][b[0]] = 1;
}

for (int i = 1; i < n; i++) {


for (int j = 1; j <= k; j++) {
int l = 0, r = 0;

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;
}
}

System.out.println(dp[n - 1][k] == 1 ? "YES" : "NO");


}
}
}

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")

SOLUTIONS - MCQ Section

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:

Angle = | 30H −(11/2)M |

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

P(Exactly two hit the target)


= P(A ∩ B ∩ C’) + P(A ∩ B’ ∩ C) + P(A’ ∩ B ∩ C) [∵ A, B and C are independent
events]
= (4/5)*(3/4)*(1/3) + (4/5)*(1/4)*(2/3) + (1/5)*(3/4)*(2/3)
= (1/5) + (1/10) + (2/15)
= 13/30

3. What is the 10th term of the series?


0, 14, 52, 126, 248, ...
A) 1998
B) 2000
C) 1456
D) 1500

Answer: A) 1998
Solution:
Initial Series:
0 14 52 126 248 …

Divide the series by 2:


0 7 26 63 124 …

Add 1 to the series:


1 8 27 64 125 …
These are cubes of natural numbers

Hence the nth term of the series is


T(n) = 2(n³ - 1)

Hence, the 10th term becomes


T(n) = 2(10³ - 1)
= 2(1000 - 1)
= 2(999)

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).

The relative speed (Vᵣₑₗₐₜᵢᵥₑ ) is given by:


vᵣₑₗₐₜᵢᵥₑ = v₁ + v₂
= 50 + 70
= 120 m/s
Now, the time (t) it takes for the trains to cross each other is given by:
= Distance / (Relative Speed)
= (l₁ + l₂) / (vᵣₑₗₐₜᵢᵥₑ)
= (80 + 120) / (120)
= 200 / 120
= 1.67 seconds

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.

On day 1, the market value decreases by x percent:

Value at the end of day 1:


= P(1 - x/100)

On day 2, the market value increases by x percent:

Value at the end of day 2:


= (P(1 - x/100))(1 + x/100)
= P(1 - x²/10000)

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,

(m-3)ⁿ-¹ = 10² = 100

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

Hence, Monika’s age 4 years ago was 18 - 4 = 14 years old.

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.

200 years contain (5 x 2) = 10 ~ 3 odd days.


Last day of 2nd century is Wednesday.

300 years contain (5 x 3) = 15 ~ 1 odd day.


Last day of 3rd century is Monday.

400 years contain 0 odd day.


Last day of 4th century is Sunday.

This cycle is repeated.


Last day of a century cannot be Tuesday or Thursday or Saturday.

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

We know that they complete the task together in 15 days.


Hence,

3x / 4 = 15
x = 20 days

15
Computer Fundamentals Section

1. What command is used to empty all contents of a table in SQL?


A) DELETE
B) DROP
C) TRUNCATE
D) EMPTY

Answer: C) TRUNCATE
Solution:
The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.

2. The Memory Buffer Register (MBR)


A) is a hardware memory device which denotes the location of the current instruction
being executed.
B) is a group of electrical circuits (hardware),that performs the intent of instructions
fetched from memory.
C) contains the address of the memory location that is to be read from or stored into.
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".

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: B) O(n log n)


Solution:
The counter of the inner loop is increasing exponentially. Hence the time it requires to reach
the end will be logarithmic. Hence, the time complexity of the inner loop will be O(log n).
The outer loop will run linearly, having time complexity of O(n). Hence, the overall time
complexity will become O(n log n).

4. Multiplicity is the same as what concept for an ERD?


A) Relationship
B) Attribute
C) Entity
D) Cardinality

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: C) Base address of the array.

6. Which of the following statements are correct about an array?


i. The array int num [26]; can store 26 elements.
ii. The expression num [1] designates the very first element in the array.
iii. It is necessary to initialize the array at the time of declaration.
iv. The declaration num [SIZE] is allowed if SIZE is a macro.
A) i
B) i, iv
C) ii, iii
D) ii, iv

Answer: B) i, iv
Solution:
The correct statements about an array are:

i. This is correct. The declaration specifies an array of integers with 26 elements.


ii. This is incorrect. In C, array indices start from 0. So, `num[0]` designates the first
element.
iii. This is incorrect. It's not necessary to initialize the array at the time of declaration. You
can declare an array and later assign values to its elements.
iv. This is correct. If `SIZE` is a macro or a constant defined before the array declaration,
then `num[SIZE]` is allowed.

So, the correct statements are i and iv.

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.

Answer: A) The startup of a thread is faster than a process.


Solution:
Creating a thread is typically faster than creating a new process. This is because a thread
shares the same resources (such as memory space) with the process that created it, while
a new process requires separate resources. Starting a new process involves more
overhead, including memory allocation, copying the program code and data, and setting up
a new execution environment.

8. Which of these items is not a form of IPC (Inter Process Communication):


A) Shared Memory
B) Pipes
C) Message Queues
D) Semaphores

Answer: None. Bonus Question.


Solution:
All the above items are a form of IPC Communication.
A) Shared Memory: It is a region of memory that is accessible to multiple processes. This
allows processes to communicate with each other by reading and writing data from the
shared memory region.
B) Pipes: A form of IPC that allows communication between processes by creating a
unidirectional communication channel.
C) Message Queues: A form of IPC that allows processes to communicate by sending and
receiving messages through a message queue.
D) Semaphores: A form of IPC that is often used for synchronization between processes.

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

Answer: C) Singleton Class


Solution:
A Singleton Class is a class that is designed to have only one instance, and it provides a
global point of access to that instance. This can be useful in scenarios where exactly one
object is needed to coordinate actions across the system, such as a configuration
manager, logging service, or a resource manager.

20

You might also like