-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClevertap Backend Engineer Hiring Challenge hackerearth question 2
More file actions
84 lines (68 loc) · 2.79 KB
/
Clevertap Backend Engineer Hiring Challenge hackerearth question 2
File metadata and controls
84 lines (68 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Question 2:
Timestamp
CleverTap’s users can access the platform via a website and a mobile app. Every time the user visits the website/mobile app, we store a timestamp of the visit.
Most of our users access the platform via the website only, however, there are a few who use both mediums to access it. Given the visit history for a particular user, our product manager would like to know how many times the user switched devices.
Example 1:
9 am, 1 Apr - Website #1
7 pm, 2 Apr - Website #2
8 pm, 2 Apr - Website #3
8 am, 7 Apr - App #4
9 am, 7 Apr - Website #5
In the example above, the user switched devices twice - once from the Website to the App (#3 to #4), and then from the App back to the Website (#4 to #5).
Example 2:
1 am, 10 Apr - App #1
7 pm, 18 Apr - Website #2
8 pm, 28 Apr - Website #3
8 am, 29 Apr - App #4
9 am, 29 Apr - Website #5
In this example, the user switched devices thrice - once from the App to the Website (#1 to #2), and then from the Website to the App (#3 to #4), and finally from the App to the Website (#4 to #5).
Function Description
You will be given the timestamps of website visits and the app visits. You’ll have to write a function computeDeviceCrossovers in the editor below. The function must return an integer denoting the number of times the given user has switched devices.
computeDeviceCrossovers has the following parameters:
websiteVisits: an array of n integers, where each websiteVisits[i] is the timestamp at which a user visited the website (sorted in ascending order)
appVisits: an array of m integers, where each appVisits[j] is the timestamp at which a user visited the app (sorted in ascending order)
Constraints
1≤n≤10,000
1≤m≤10,000
Testcase
A number n - followed by n lines - each line denoting website visit timestamp
A number m, following by m lines - each line denoting app visit timestamp
#code
def computeDeviceCrossovers (n, websiteVisits, m, appVisits):
if(n==0 or n==0):
return 0
else:
l=[]
l.extend(websiteVisits)
l.extend(appVisits)
l.sort()
d={}
c=0
for i in range(0,len(l)):
if(l[i] in websiteVisits):
d[l[i]]='w'
if(l[i] in appVisits):
d[l[i]]='a'
newl=list(d.values())
#print(newl)
for i in range(0,len(newl)):
if(i==0):
prev=newl[i]
else:
cur=newl[i]
if(prev!=cur):
c=c+1
prev=cur
return c
n = int(input())
websiteVisits = []
appVisits = []
for i in range(0,n):
temp = int(input())
websiteVisits.append(temp)
m = int(input())
for i in range(0,m):
temp = int(input())
appVisits.append(temp)
out_ = computeDeviceCrossovers(n, websiteVisits, m, appVisits)
print(out_)