-
Notifications
You must be signed in to change notification settings - Fork 0
/
actHQassignment.js
216 lines (186 loc) · 5.72 KB
/
actHQassignment.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Challenge
// Given a specific dataset representing company information and a set of changes that have accumulated over a period of time, write a program that takes a timestamp and query as input, and gives a YES/NO output based on whether the query succeeds or fails based on the underlying data.
// Outlined below is a representative sample containing data for a Company and some query samples.
// Company Data
// Company = {
// name: "ActHQ",
// legal_name: "ActHQ Inc.",
// domain: "acthq.com",
// url: "https://acthq.com",
// type: "Private",
// founded_year: 2022,
// headcount: 2,
// location: [{
// city: "San Francisco",
// state: "California",
// country: "United State"
// }],
// timestamp: "2022-01-30"
// }
// Delta = [
// // Company grew headcount & added social media profiles
// {
// headcount: 4,
// social_profiles: {
// twitter: "https://twitter.com/ActHQ",
// linkedin: "https://www.linkedin.com/company/act-hq"
// },
// timestamp: "2022-06-12"
// },
// // Company raised the seed round
// {
// funding: {
// amount: "1500000",
// currency: “USD”,
// round: "Seed"
// },
// timestamp: "2023-01-25"
// },
// // Company raised Series A and grew headcount
// {
// headcount: 10,
// funding: {
// amount: "5000000",
// currency: “USD”,
// round: "Series A"
// },
// timestamp: "2024-10-08"
// }
// ]
// Query Samples
// // Did the company raise a seed round by the end of March 2022?
// Query:
// {
// funding: {
// round: “Seed”
// },
// timestamp: “2022-03-31”
// }
// Answer: NO
// // Did the company raise a seed round by the end of Jan 2023 and is headquartered in San Francisco?
// Query:
// {
// funding: {
// round: “Seed”
// },
// location: {
// city: “San Francisco”
// },
// timestamp: “2023-01-31”
// }
// Answer: YES
// // Was the company’s headcount less than 10 by the end of the year 2022?
// Query:
// {
// headcount: {
// min: 0,
// max: 10
// },
// timestamp: “2022-12-31”
// }
// Answer: YES
// // Did the company go public by the end of the year 2024?
// Query:
// {
// type: “Public”,
// timestamp: “2024-12-31”
// }
// Answer: NO
// Notes
// Please use a programming language of your choice to represent the data and logic
// It’s advisable to come up with alternative implementations with varying degrees of complexity
// You should be able to clearly explain the memory and time complexities of different implementations
// (Assume that the company & query objects both can have a maximum of A attributes, each with at most N-levels of nesting. The input stream of Delta changes consist of D separate streams, each having maximum of A attributes, with at most N-levels of nesting)
// Although real working code would be preferable, we would be more interested in the approach you took as compared to the accuracy of the output
// Constraints
// What would be the efficient way to store and query this kind of data?
// How would your solution change when the delta changes are not a static set, instead these are provided as data-streams happening as time goes by?
// How would you handle complex queries involving multiple attributes with nested AND/OR/NOT conditions in an efficient way?
// Assuming there are millions of such companies and they have thousands of streaming changes on average over the course of time, how would the data storage, comparison and query look like?
comapnyData = {
legal_name: "ActHQ Inc.",
domain: "acthq.com",
url: "https://acthq.com",
type: "Private",
founded_year: 2022,
headcount: 2,
location: [
{
city: "San Francisco",
state: "California",
country: "United State",
},
],
timestamp: "2022-01-30",
};
Delta = [
// Company grew headcount & added social media profiles
{
headcount: 4,
social_profiles: {
twitter: "https://twitter.com/ActHQ",
linkedin: "https://www.linkedin.com/company/act-hq",
},
timestamp: "2022-06-12",
},
// Company raised the seed round
{
funding: {
amount: "1500000",
currency: "USD",
round: "Seed",
},
timestamp: "2023-01-25",
},
// Company raised Series A and grew headcount
{
headcount: 10,
funding: {
amount: "5000000",
currency: "USD",
round: "Series A",
},
timestamp: "2024-10-08",
},
];
function query(queryObj) {
const keys = Object.keys(queryObj);
// Check each item in delta with timestamp less than query timestamp
for (let i = 0; i < Delta.length; i++) {
if (Delta[i].timestamp < queryObj.timestamp) {
for (let j = 0; j < keys.length; j++) {
if (keys[j] === "headcount" && Delta[i].headcount) {
if (
Delta[i].headcount > queryObj.headcount.min &&
Delta[i].headcount < queryObj.headcount.max
) {
return true;
}
} else if (keys[j] === "funding" && Delta[i].funding) {
if (Delta[i].funding.round === queryObj.funding.round) {
return true;
}
} else if (keys[j] === "location" && Delta[i].location) {
if (Delta[i].location.city === queryObj.location.city) {
return true;
}
} else if (keys[j] === "type" && Delta[i].type) {
if (Delta[i].type === queryObj.type) {
return true;
}
}
}
}
}
return false;
}
console.log(query({ funding: { round: "Seed" }, timestamp: "2022-03-31" })); // false
console.log(
query({
funding: { round: "Seed" },
location: { city: "San Francisco" },
timestamp: "2023-01-31",
})
); // true
console.log(query({ headcount: { min: 0, max: 10 }, timestamp: "2022-12-31" })); // true
console.log(query({ type: "Public", timestamp: "2024-12-31" })); // false