Skip to content

Commit 87ba408

Browse files
committed
added: barclays plc uk - online assessment
1 parent 06565fb commit 87ba408

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""
2+
Allie is working on a system that can allocate resources to the
3+
applications in a manner efficient enough to allow the maximum number
4+
of applications to be executed. There are N number of applications
5+
and each application is identified by a unique integer ID (1 to N).
6+
Only M types of resources are available with a unique resourceD.
7+
Each application sends a request message to the system.
8+
The request message includes the information regarding the request time,
9+
the execution ending time, and the type of resource required for execution.
10+
Time is in the MMSS format where MM is minutes and SS is seconds.
11+
12+
If more than one application sends a request at the same time then only
13+
one application will be approved by the system. The denied requests are
14+
automatically destroyed by the system. When approving the request, the
15+
system ensures that the request will be granted to the application in a
16+
way that will maximize the number of executions. The system can execute
17+
only one application at a time with a given resource. It will deny all
18+
other requests for the resource until the previous application has finished.
19+
Allie wants to know the maximum number of applications that have been
20+
executed successfully.
21+
22+
Write an algorithm to help Allie calculate the maximum number of applications
23+
that are executed successfully by the system.
24+
25+
Input
26+
The first line of the input consists of two space-separated integers num and
27+
constX, representing the number of applications (N) and constX is always 3.
28+
The next N lines consist of constX space-separated integers representing the
29+
request time, the execution ending time, and the resourceD of the resource
30+
required by each application for successful execution.
31+
32+
Output
33+
Print an integer representing the maximum number of applications that are
34+
executed successfully by the system.
35+
36+
37+
Testcase 1 | Answer: 4
38+
4 3
39+
1000 1020 3
40+
1020 1030 3
41+
1030 1040 3
42+
1010 1045 2
43+
44+
Testcase 2 | Ans: 3
45+
5 3
46+
1200 1230 1
47+
1120 1125 2
48+
1015 1230 1
49+
1100 1230 1
50+
1200 1230 3
51+
52+
Testcase 3 | Ans: 4
53+
6 3
54+
1200 1250 1
55+
1210 1220 1
56+
1225 1230 1
57+
1330 1345 2
58+
1330 1340 2
59+
1340 1345 2
60+
"""
61+
62+
63+
# to bucket all requests by resource type
64+
def bucketRequestsByResource(arr):
65+
buckets = dict()
66+
for each_req in arr:
67+
if buckets.get(each_req[2], False) != False:
68+
buckets[each_req[2]].append((each_req[0], each_req[1]))
69+
else:
70+
buckets[each_req[2]] = [(each_req[0], each_req[1])]
71+
72+
return buckets
73+
74+
75+
# to get max number of executed tasks for a single bucket
76+
def numExecutedAppsByBucket(arr):
77+
arr.sort(key = lambda x: x[0])
78+
N = len(arr)
79+
dont_execute = 0
80+
latest_end = arr[0][1]
81+
82+
for i in range(1, N):
83+
if arr[i][0] < latest_end:
84+
dont_execute += 1
85+
latest_end = min(arr[i][1], latest_end)
86+
else:
87+
latest_end = arr[i][1]
88+
89+
return (N - dont_execute)
90+
91+
92+
# get the maximum number of executed tasks
93+
def numExecutedApps(arr):
94+
buckets = bucketRequestsByResource(arr)
95+
num_execute = 0
96+
for each_bucket in buckets.values():
97+
num_execute += numExecutedAppsByBucket(each_bucket)
98+
99+
return num_execute
100+
101+
102+
# driver code
103+
arr = []
104+
arr_rows, arr_cols = map(int, input().split())
105+
for idx in range(arr_rows):
106+
arr.append(list(map(int, input().split())))
107+
108+
result = numExecutedApps(arr)
109+
print (result)
110+

0 commit comments

Comments
 (0)