-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterviewQuestions.cpp
292 lines (267 loc) · 8.99 KB
/
InterviewQuestions.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <list>
#include <array>
#include <map>
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include <math.h>
// Just adding for convenience for the purpose of this exercise
using namespace std;
/*
* Given a string, write a method which returns a set of the most common
* characters (could be more than one)
* For example:
* In word “annoying”, the most common character is {‘n’}
* In word “implementation”, the most common characters are
* {‘i’, ‘m’, ‘e’, ‘t’, ‘n’}
* You can assume the following method signature:
*
* void GetMostUsedCharacters(string input, List<char> array)
*/
void GetMostUsedCharacters(const string& input, list<char>& array)
{
// Create a map holding a count of the number of times each character
// appears in the string. And also record the maximum frequency of any
// character
int max_frequency = 0;
map<char,int>occurrences;
for(int i=0; i<input.size(); ++i)
{
occurrences[input[i]] =
occurrences.count(input[i]) == 0? 1: occurrences[input[i]] + 1;
int freq = occurrences[input[i]];
if(freq > max_frequency)
{
max_frequency = freq;
}
}
// Iterate through occurrences and if any given characters frequency is
// equal to max frequency, then add that character to array
for(map<char,int>::iterator iter=occurrences.begin();
iter != occurrences.end();
iter ++)
{
if(iter->second == max_frequency)
{
array.push_back(iter->first);
}
}
}
/*
* Consider an array containing all unique integers 1 to 100 in random order.
* If a random array element is set to 0, write a method that determines which
* number has been removed. You can assume the following method signature :
*
* int GetNumberMissing(int[] array)
*/
int GetNumberMissing(int array[])
{
int n = 100;
// Compute the total sum of all the numbers in the range
// Without using magic numbers
int total = n*(n+1)/2;
// Subtract each element of the array from the total
// The remainder is the missing number
for(int i=0; i<100; ++i)
{
total -= array[i];
}
return total;
}
/*
* Say you have an array for which the ith element is the price of a given stock
* on day i. If you were only permitted to complete at most one transaction
* (for example, buy one AND sell one share of the stock), design an algorithm
* to find the maximum profit. The method should return the maximum profit and
* also a summary of the transactions
* You can assume the following method signature :
*
* void GetMaxProfit(vector<double> &prices,array<int,2> &transactions, double &profit)
*/
void GetMaxProfit(const vector<double> &prices
,array<int,2> &transactions
,double &profit)
{
// Initialise array elements to -1,-1
transactions = {-1,-1};
// Iterate through the array of prices keeping track of the max and min values
// Each time we find a new max price - recalculate max profit
// Each time we find a new min price - reset the max. We need to find a max,
// which occurs after the min
double min = prices[0];
double max = prices[0];
int min_idx = 0;
int max_idx = 0;
for(size_t i=1; i<prices.size(); ++i)
{
if(prices[i] < min)
{
min = prices[i];
min_idx = i;
max = min;
}
else if(prices[i] > max)
{
max = prices[i];
max_idx = i;
if((max - min) > profit)
{
// Round the profit to two decimal places and eliminate floating
// Point errors
profit = double(int(max *100) - int(min *100))/100;
transactions[0] = min_idx;
transactions[1] = max_idx;
}
}
}
}
// Helper function to convert a date string of format dd/mm/yyyy to an integer
// The integer returned is constructed as follows
// return_value = year*10K + month *100 + day
// For example 10/02/2017 will be converted to 20170210
// Note that integers returned are used for comparison purposes only.
inline int DateToInt(const string& date)
{
int day = stoi(date.substr(0,2));
int month = stoi(date.substr(3,5));
int year = stoi(date.substr(6));
return ((year *10000) + (month *100) + day);
}
// Helper function to convert an integer to a date of format dd/mm/yyyy
// The integer to be converted should be of the same format as integers
// by DateToInt function
// For example 20170210 will be converted to 10/02/2017
inline string IntToDate(int date)
{
int year = date/10000;
date -= year * 10000;
int month = date/100;
int day = date - month * 100;
return(to_string(day) + "/" + to_string(month) + "/" + to_string(year));
}
/*
* Assume you have a list of pairs of start and end dates.
* Write a method that will return the maximum coverage of the list.
* For example, assume list of pairs,
* {(01/01/2016, 03/01/2016), (02/01/2016, 06/01/2016),
* (07/01/2016, 10/01/2016), (08/01/2016, 16/01/2016)},
* then the method should return the following intervals,
* {(01/01/2016, 06/01/2016), (07/01/2016, 16/01/2016)}.
*/
void GetMaxCoverage(const vector<pair<string,string>> &dates,
vector<pair<string,string>> &maxCoverage)
{
int length = dates.size();
// return shared_ptr to an empty vector if dates vector is empty
if(length == 0)
{
return;
}
// Convert dates to 2D list of start and end dates and
// Sort by start date in ascending order
// Note that date strings are converted to integers using DateToInt()
// helper function defined above
vector<vector<int>> sortedDates;
for(size_t i=0; i<length; i++)
{
sortedDates.push_back({DateToInt(dates[i].first)
,DateToInt(dates[i].second)});
}
sort(sortedDates.begin(), sortedDates.end());
// Make a list of pairs of the max overlapping dates
int start = sortedDates[0][0];
int end = sortedDates[0][1];
for(size_t i=0; i<length; ++i)
{
if(sortedDates[i][0] > end)
{
maxCoverage.push_back(make_pair(IntToDate(start),IntToDate(end)));
start = sortedDates[i][0];
end = sortedDates[i][1];
}
else if(sortedDates[i][1] > end)
{
end = sortedDates[i][1];
}
}
maxCoverage.push_back(make_pair(IntToDate(start),IntToDate(end)));
}
/*
* Suppose you are given an array of integers. Write a method that maximizes
* the sum of a subset such that if you include the number in the sum,
* you may not use the adjacent numbers (the numbers immediately to the left
* or right of that number) to count in the sum.
* For example:
* int A [ ] = {1, - 1, 3, 8 ,4 } the max sum is 1+ 8 = 9;
* int A [ ] = { 1,2,3,4, 5} the max sum is 1 + 3 + 5 = 9;
* The method should return both the subset and the max sum.
* You can assume the following method signature:
*
* void GetMaxSum(ref int maxSum, List<int> subset)
*
* The first argument is passed by reference so that the method can write the
* maximum sum and the second argument is a list of integers which make up the
* maximum sum.
*/
void GetMaxSum(vector<int> &input,int &sum, vector<int>& subset)
{
//return if the input is empty
if(input.size() == 0)
{
return;
}
// Iterate through the input array
// Calculate the sum of sets including the last element
// And the sum of sets excluding the last element
// If the sum of sets including the last element is larger than the sum of
// sets excluding - continue iteration
// Otherwise - The last element cannot appear in our set. Therefore we can
// populate the values of subset up until this point
int incl = input[0];
int excl = 0;
int excl_next;
int incl_count = 0;
int start_idx = 0;
for (size_t i = 1; i < input.size(); i++)
{
if(incl > excl)
{
++incl_count;
excl_next = incl;
}
else
{
// populate the values of subset up until this point
// If we have an even number of includes then we start at
// start_idx + 1 otherwise at start_idx
start_idx = incl_count %2 == 1? start_idx: start_idx +1;
for(size_t j= start_idx; j< (start_idx + incl_count); j=j+2)
{
subset.push_back(input[j]);
}
// reset incl_count and start_idx
incl_count = 0;
start_idx = i;
// The next value of excl will not change
excl_next = excl;
}
incl = excl + input[i];
excl = excl_next;
}
// Repeat everything one last time to process the last element in the set
if(incl > excl)
{
++incl_count;
}
else
{
incl = excl;
}
sum = incl;
start_idx = incl_count %2 == 1? start_idx: start_idx +1;
for(size_t j= start_idx; j< (start_idx + incl_count); j=j+2)
{
subset.push_back(input[j]);
}
}