Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterviewQuestions.cpp
More file actions
547 lines (443 loc) · 13.9 KB
/
Copy pathinterviewQuestions.cpp
File metadata and controls
547 lines (443 loc) · 13.9 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// --------------- 1 zox
/*
Steering Problem
A controls engineer has collected test data for a black box steering system to
determine the amount of voltage that needs to be applied to hold the vehicle at a
given steering angle. The data below shows that the voltage (V)
required is a function of the steering angle (a).
Test Data:
a = -22, -11, 0, 10, 20 degrees
V = -1.5, -1, 0, 1.2, 1.8 volts
The controls engineer would like to use this data as a feed forward term for the
steering controller. Your job is to write a function to determine
the best estimate for voltage (V) given a steering angle (a). aka. V = f(a).
Example output:
For a = 10; V = 1.2
For a = 5; V = 0.6
For a = 15; V = 1.5
This problem uses a unit test framework called Catch. Use the example tests at the
bottom of the file to test voltageEstimate, and feel free to add your own as well.
*/
// Unit testing framework
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
// Standard includes
#include <iostream>
#include <stdint.h>
// y1 = m * x1 + c
// y2 = m * x2 + c
// m = (y1 - y2) / (x1 - x2)
float voltageEstimate(const float a, const float *a_data, float *v_data, size_t size /* TODO */) {
// TODO
float ratio;
// float c = 0;
for(int i = 0; i < size - 1; i++) {
if(a >= a_data[i] && a <= a_data[i + 1]) {
ratio = (v_data[i + 1] - v_data[i]) / (a_data[i + 1] - a_data[i]);
// c = v_data[i];
break;
}
}
// return a * ratio + c;
return a * ratio;
}
TEST_CASE( "Steering Problem Test" ) {
const float a_data[] = {-22, -11, 0, 10, 20}; // x-axis data
const float v_data[] = {-1.5, -1, 0, 1.2, 1.8}; // y-axis data
const int size = 5;
float a, v_expected, v_test;
SECTION( "Example 1" ) {
// input angle
a = 10.0;
// expected output voltage
v_expected = 1.2;
// call function under test
v_test = voltageEstimate(a, a_data, v_data, size);
// check that our result is correct (within floating point error)
REQUIRE(v_expected == Approx(v_test));
}
SECTION( "Example 2" ) {
// input angle
a = 15.0;
// expected output voltage
v_expected = 1.5;
// call function under test
v_test = voltageEstimate(a, a_data, v_data, size);
// check that our result is correct (within floating point error)
REQUIRE(v_expected == Approx(v_test));
}
// TODO: Add any new test sections here
}
// --------------- 2
/*
Convert Temperature Sensor Data
Description:
We have a temperature sensor connected to an MCU via an I2C bus. The temperature sensor provides data in one of two measurement modes - standard mode or extended mode. In standard mode, the sensor can measure temperature in the range from 0°C to 127°C, and in the extended mode, it can measure temperature in the range from -64°C to 191°C.
In any mode, the MCU receives 24-bits of data. A description of the data is given below:
The high byte, bits 15 to 8, contains the integer portion of the temperature in °C, and the low byte, bits 7 to 0, contains the decimal fraction of the temperature in °C.
In standard mode, temperatures lower than 0°C are reported as 0°C by the sensor; similarly, temperatures higher than 127°C are reported as 127°C. In extended mode, the sensor reports temperatures from -64°C to 191°C.
Function Description:
The goal of this question is to populate the function convert_to_temp. This function takes in one argument input_val and returns the converted temperature value in degrees centigrade.
Constraints:
The input decimal value will be within the conversion range of the sensor.
*/
Input Format For Custom Testing
Sample Case 0
Sample Case 1
C
res = input_val >> 8;
res &= (~((1 << 16) - 1));
FracPart = calFract(input_val);
} else {
res = input_val >> 8;
res &= (~((1 << 15) - 1));
uint32_t low = (1 << 8) - 1;
uint32_t high = (1 << 16) - 1;
uint32_t mask = high - low;
int intPart = (input_val & mask) >> 15;
Line: 65 Col: 26
Run Code
Run Tests
Input / Output
Test Cases
Input
29552
Run Code to see your output here.
double __y, hint
// --------------- 3
// google
// 1. implement context grep
#include <iostream>
#include <string>
char *argv[] = {
"Hello world",
"Welcome to California",
"Goodbye",
"Big sky",
"Nice job",
"Blue sky",
"Hey Joe",
};
void context_grep(int argc, char **argv, int context, char *expr) {
if(argc <= 0 || \
argv == nullptr || \
expr == nullptr || \
context < 0) {
cout << "Invalid input!" << endl;
return;
}
int firstTime = -1;
int lastTime = -1;
for(int i = 0; i < argc; i++) {
if(strstr(argv[i], expr) != nullptr) {
if(firstTime == -1) {
firstTime = i;
}
lastTime = i;
}
}
if(firstTime == - 1) return;
// copy the strings
vector<string> res;
for(int i = firstTime; i <= lastTime; i++) {
string tmp = argv[i];
res.push_back(tmp);
}
// copy the front context
for(int i = context; i > 0 && firstTime-- > 0; i--) {
res.insert(res.begin(), argv[firstTime]);
}
// copy the rear context
for(int i = 0; i < context && lastTime++ < argc; i--) {
res.push_back(argv[lastTime]);
}
for(auto x : res) {
cout << x << endl;
}
}
int main() {
// case 1
context_grep(6, argv, 2, "sky");
// case 2: no print
context_grep(6, argv, -1, "sky");
// case 3:
context_grep(6, argv, 1, "sky");
// case 4:
context_grep(6, argv, 1, "sy");
// case 5:
context_grep(0, argv, 1, "sy");
// case 6:
context_grep(-1, argv, 1, "sy");
// case 7:
context_grep(6, nullptr, 1, "sy");
// case 8:
context_grep(6, argv, 1, nullptr);
// case 9:
context_grep(6, argv, 11, "sky");
// case 10:
context_grep(6, argv, 0, "sky");
return 0;
}
// --------------- 4
// 2. reverse the order of words in a string
void reverseStr(string& s, int left, int right){
for (int i = left, j = right; i < j; i++, j--) {
swap(s[i], s[j]);
}
}
void removeSpaces(string& s) {
int slow = 0;
for (int i = 0; i < s.size(); ++i) { //
if (s[i] != ' ') {
if (slow != 0) {
s[slow++] = ' ';
}
while (i < s.size() && s[i] != ' ') { // 不等于空格的情况
s[slow++] = s[i++];
}
}
}
s.resize(slow);
}
string reverseWords(string s) {
if(s.empty()) {
return "";
}
removeSpaces(s);
reverseStr(s, 0, s.size() - 1);
int start = 0;
for (int i = 0; i <= s.size(); ++i) {
if (i == s.size() || s[i] == ' ') {
reverseStr(s, start, i - 1);
start = i + 1;
}
}
return s;
}
int main(void) {
// case 1
string s1 = "I love San Diego";
// case 2
string s2 = "I love San Diego ";
// case 3
string s3 = " I love San Diego!";
// case 4
string s4 = "";
// case 5
string s5 = " I love San Diego!"
string s = reverseWords(s3);
cout << s << endl;
}
// --------------- 5
// 3. Write functions to insert and search for an element in a binary search tree
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
TreeNode(int val) : value(val), left(nullptr), right(nullptr) {}
};
// Function to insert a value into the BST
TreeNode* insert(TreeNode* root, int value) {
if (root == nullptr) {
return new TreeNode(value);
}
if (value < root->value) {
root->left = insert(root->left, value);
} else if (value > root->value) {
root->right = insert(root->right, value);
}
return root;
}
// Function to search for a value in the BST
bool search(TreeNode* root, int value) {
if (root == nullptr) {
return false;
}
if (value == root->value) {
return true;
} else if (value < root->value) {
return search(root->left, value);
} else {
return search(root->right, value);
}
}
int main() {
TreeNode* root = nullptr;
// case 1: Insert values into the BST
root = insert(root, 5);
root = insert(root, 3);
root = insert(root, 7);
root = insert(root, 2);
root = insert(root, 4);
root = insert(root, 6);
root = insert(root, 8);
// case 2: Search for values in the BST
std::cout << "Searching for 6: " << (search(root, 6) ? "Found" : "Not found") << std::endl;
// case 3: Search for values in the BST
std::cout << "Searching for -1: " << (search(root, -1) ? "Found" : "Not found") << std::endl;
// case 4: Search for values in the BST
std::cout << "Searching for 9: " << (search(root, 9) ? "Found" : "Not found") << std::endl;
return 0;
}
// --------------- 6
/*
Implement a blurring effect on an image that is represented as a MxN matrix of Pixels.
Blurring a single pixel is done by averaging values in surrounding area. Neighborhood area AxB
Example:
Neighborhood area: 3X5
Input:
___ ___ ___ ___ ___ ___ ___ _...
|_1_|_5_|_2_|_3_|_5_|_6_|_1_|_...
|_2_|_1_|_1_|_9_|_4_|_8_|_3_|_...
|_1_|_1_|_2_|_3_|_5_|_2_|_9_|_...
|___|___|___|___|___|___|___|_...
|...|...|...|...|...|...|...|_...
Output (only few pixels are blurred)
0_|_ ___ ___ ___ ___ ___ ___ _
0 |_x_|___|___|___|___|___|___|_...
0 |_ _|___|_3_|_4_|___|___|___|_...
0 |___|___|___|___|___|___|___|_...
|___|___|___|___|___|___|___|_...
0 |...|...|...|...|...|...|...|_...
*/
int sum(int **arr, int row, int col, int posX, int posY, int a, int b) {
int i, j, sum = 0;
int coutX = 0;
int coutY = 0;
a = a >> 1;
b = b >> 1;
for(i = posX - a; i <= posX + a; i++) {
coutX = 0;
coutY = 0;
for(j = posY - b; j <= posY + b; j++) {
if(i < 0 || i >= row || j < 0 || j >= col) {
continue;
// arr[i][j] = 0; // arr[-1][-2] = 0;
}
coutX++;
sum += arr[i][j];
}
coutY++;
}
sum /= (coutX * coutY);
return sum;
}
// 0x00 0x01
// 0x
int blurring(void *arr, void *res, int row, int col, int a, int b) {
if(!arr) {
return -1;
}
// int res[row][col] = {0};
int (*arr_)[col] = arr;
int (*res_)[col] = res;
// 3 X 5
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
res_[i][j] = sum(arr_, row, col, i, j, a, b);
}
}
return 0;
}
// ----------------
typedef struct data {
char x;
int y;
} Data;
Data a, b;
assert(a.x == b.x); // pass
assert(a.y == b.y); // pass
assert(a == b); // failed. why?
answer: the structure elements will be aligned in memory.
Fill char x(1 byte) to 4 bytes with random contents. so a may not equal to b
// --------------- 7
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iostream>
// Hysteresis
//
// The function below is called periodically from a task which sets a heater's
// power to on or off. The heater uses a relay, which shouldn't be cycled too
// rapidly. To avoid this, rather than turning the heater on and off at a
// specific set-point, we need a function which turns the heater on when the
// heater temperature drops more than 2 degrees below the set point, and turns
// it off when the heater temperature exceeds the set point by more than 2
// degrees. If the function is called with the heater temperature within the
// window, bounded by the high and low thresholds, the current state should be
// preserved. Regardless, the return value of this function will be used as an
// input to turn on or off the relay (i.e. it should reflect the current
// requested relay state).
#include <cstdint>
#define SET_POINT_C ((uint32_t)24)
// time 1: current 100 -> (24+2 < 100)
// time 2: 12
//task will call function ~100ms (10x a sec)
// would prefer lock around call & set of relay.
void enable_heater(volatile int32_t* current_temp, const uint32_t thre, bool* on) {
pthread_mutex_lock(); //
// 1. compare the current_tmp with set_point_c
static bool enable_heater = false; // false is a good start value
int32_t diff = *current_temp - SET_POINT_C;
// 2. if diff <= 2
if(diff > thre)
*on = true;
// enable relay here.
if(diff < -thre)
return enable_heater;
// return if "in the window"
// previous call may have turned on, or turned of, the heater.
return enable_heater;
}
//
//uint8_t reg; // memory mapped register
set_bit2(volitle uint8_t* reg)
{
*reg |= (1 << 2);
}
clear_bit2(volitle uint8_t* reg)
{
*reg &= ~(1 << 2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
一个flash 128 KBit ( 128 * 1024 bits),根据start_address和length来设定mask:
1. start_address 和 length都是4k aligned.
2. 例子:
1).start_address: 0 ~ 4095, length: 4096
mask: 0000,0000,0000,0000,0000,0000,0000,0001
2).start_address: 0 ~ 4095, length: 4096 * 2
mask: 0000,0000,0000,0000,0000,0000,0000,0011
3).start_address: 4096 ~ 4096 * 2, length: 4096 * 2
mask: 0000,0000,0000,0000,0000,0000,0000,0110
*/
/*
分析: 128 KBit / 4096 = 32位
1. 先根据start_address来找到起始位,
2. 然后再根据长度来找到要置位的长度,
3. 然后从start_address来根据置位长度来置位
*/
uint32_t setMask(uint32_t start_address, uint32_t length) {
uint32_t startBit = 0;
// for(int i = 0; i < 128 * 1024 - 4096; i += 4096) {
// if(start_address > i) {
// startBit++;
// }
// }
// or
startBit = start_address / 4096 + 1;
uint32_t bitsLen = 0;
// for(int i = 0; i < 128 * 1024 - 4096; i += 4096) {
// if(start_address > i) {
// bitsLen++;
// }
// }
// or
bitsLen = length / 4096 + 1;
uint32_t mask = 0;
for(int i = 0; i < bitsLen; i++) {
mask |= (1 << (startBits + i));
}
return mask;
}
You can’t perform that action at this time.
