-
Notifications
You must be signed in to change notification settings - Fork 0
/
whiteboard_coding.cpp
344 lines (268 loc) · 8.34 KB
/
whiteboard_coding.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
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
#include <iostream>
#include <set>
#include <string>
using namespace std;
void printHeader() {
cout << endl << "#####################################" << endl;
}
// Swap values without a temporary variable if the operands support xor.
// This may not be faster than letting the compiler optimze out a temporary
// variable, but it's still something often used in interview questions.
template <typename DataType>
void xorSwap(DataType* one, DataType* two) {
if (one != two) {
*one ^= *two;
*two ^= *one;
*one ^= *two;
}
}
// Swap values with a temporary variable
template <typename DataType>
void swap(DataType* one, DataType* two) {
if (!one || !two) {
return;
}
DataType temp(*one);
*one = *two;
*two = temp;
}
void reverseString(char* aString) {
// Find the last character
char* endChar = aString;
while(*(endChar + 1) != '\0') {
endChar++;
}
// Reverse the string in place
//while (endChar != aString && endChar != aString - 1) {
while (endChar > aString) {
//swap<char>(aString, endChar);
xorSwap<char>(aString, endChar);
aString++;
endChar--;
}
}
template <typename DataType>
void printArray(DataType* array, int arrayCount) {
for (int index = 0; index < arrayCount; index++) {
if (index != 0) {
cout << ", ";
}
cout << array[index];
}
}
void mergeSortedIntArrays(int intArray1[], int array1Count,
int intArray2[], int array2Count, int outputArray[]) {
int array1Index = 0;
int array2Index = 0;
int outputIndex = 0;
while (array1Index != array1Count || array2Index != array2Count) {
if (array1Index == array1Count) {
// Nothing left in intArray1
outputArray[outputIndex] = intArray2[array2Index];
array2Index++;
} else if (array2Index == array2Count) {
// Nothing left in intArray2
outputArray[outputIndex] = intArray1[array1Index];
array1Index++;
} else {
if (intArray1[array1Index] < intArray2[array2Index]) {
outputArray[outputIndex] = intArray1[array1Index];
array1Index++;
} else {
outputArray[outputIndex] = intArray2[array2Index];
array2Index++;
}
}
outputIndex++;
}
}
unsigned int myStrLen(char* aString) {
unsigned int length = 0;
if (aString) {
while (*aString != '\0') {
length++;
aString++;
}
}
return length;
}
void myStrCpy(char* destString, const char* srcString) {
if (!destString || !srcString) {
return;
}
while (*srcString != '\0') {
*destString = *srcString;
srcString++;
destString++;
}
*destString = '\0';
}
string addSpacesToStr(const set<string>& words, const string& withoutSpaces) {
for (unsigned int charIndex = 0;
charIndex <= withoutSpaces.length();
charIndex++) {
string wordOne = withoutSpaces.substr(0, charIndex);
string wordTwo = withoutSpaces.substr(charIndex,
withoutSpaces.length() - charIndex);
if (words.find(wordOne) != words.end() &&
words.find(wordTwo) != words.end()) {
return wordOne + " " + wordTwo;
}
}
return withoutSpaces;
}
int fibonacciIterative(int iteration) {
if (iteration == 0) {
return 0;
} else if (iteration == 1) {
return 1;
}
int prevPrev = 0;
int prev = 1;
int result = 0;
for (int index = 2; index <= iteration; index++) {
result = prevPrev + prev;
prevPrev = prev;
prev = result;
}
return result;
}
int fibonacciRecursive(int iteration) {
if (iteration == 0) {
return 0;
} else if (iteration == 1) {
return 1;
}
return fibonacciRecursive(iteration - 1) +
fibonacciRecursive(iteration - 2);
}
int main(int /*argc*/, char** /*argv*/) {
//
// Let the user know we're starting.
//
cout << endl << "Beginning Whiteboard Coding." << endl;
//
// Reverse a string with an even number of characters
//
printHeader();
cout << endl << "Reverse String (Even Chars)" << endl;
char* evenString = "Backwards Day (Even)";
cout << "Original: " << evenString << endl;
reverseString(evenString);
cout << "Reversed: " << evenString << endl;
//
// Reverse a string with an odd number of characters
//
printHeader();
cout << endl << "Reverse String (Odd Chars)" << endl;
char* oddString = "Backwards Day (Odd)";
cout << "Original: " << oddString << endl;
reverseString(oddString);
cout << "Reversed: " << oddString << endl;
//
// Calculate the length of a null terminated char*
//
printHeader();
cout << endl << "Implement strlen" << endl;
char* lenString1 = "This is a test string.";
cout << endl;
cout << "String : " << lenString1 << endl;
cout << "Expected: 22" << endl;
cout << "Actual : " << myStrLen(lenString1) << endl;
char* lenString2 = "";
cout << endl;
cout << "String : <Empty String>" << endl;
cout << "Expected: 0" << endl;
cout << "Actual : " << myStrLen(lenString2) << endl;
char* lenString3 = 0;
cout << endl;
cout << "String : NULL" << endl;
cout << "Expected: 0" << endl;
cout << "Actual : " << myStrLen(lenString3) << endl;
//
// Implement strcpy
//
printHeader();
cout << endl << "Implement strcpy" << endl;
cout << endl;
char* srcString1 = "This is a test string.";
cout << "Source String: " << srcString1 << endl;
char* destString1 = new char[myStrLen(srcString1) + 1];
myStrCpy(destString1, srcString1);
cout << "Copied String: " << destString1 << endl;
delete [] destString1;
cout << endl;
char* srcString2 = "";
cout << "Source String: <Empty String>" << endl;
char* destString2 = new char[myStrLen(srcString2) + 1];
myStrCpy(destString2, srcString2);
cout << "Copied String: " << destString2 << endl;
delete [] destString2;
//
// Merge two sorted int arrays into one sorted int array
//
printHeader();
cout << endl << "Merge Sorted Int Arrays" << endl;
int intArray1[] = {1, 3, 6, 8, 10};
const int array1Count = sizeof(intArray1) / sizeof(intArray1[0]);
cout << "Array1 : ";
printArray(intArray1, array1Count);
cout << endl;
int intArray2[] = {2, 3, 5, 9, 9};
const int array2Count = sizeof(intArray2) / sizeof(intArray2[0]);
cout << "Array2 : ";
printArray(intArray2, array2Count);
cout << endl;
int outputArray[array1Count + array2Count];
cout << "Expected: 1, 2, 3, 3, 5, 6, 8, 9, 9, 10" << endl;
mergeSortedIntArrays(intArray1, array1Count, intArray2, array2Count,
outputArray);
cout << "Actual : ";
printArray(outputArray, array1Count + array2Count);
cout << endl;
//
// Split a string of words without spaces into a string of words with spaces
//
printHeader();
cout << endl << "Adding Spaces To String Of Words" << endl;
set<string> wordSet;
wordSet.insert("pea");
wordSet.insert("nut");
wordSet.insert("peanut");
wordSet.insert("but");
wordSet.insert("butter");
wordSet.insert("awesome");
string withoutSpacesStr("peanutbutter");
cout << endl;
cout << "Words Without Spaces: " << withoutSpacesStr << endl;
string withSpacesStr = addSpacesToStr(wordSet, withoutSpacesStr);
cout << "Words With Spaces : " << withSpacesStr << endl;
//
// Fibonacci
//
printHeader();
int iterations = 15;
cout << endl << "First " << iterations <<
" Numbers Of Fibonacci Sequence" << endl;
cout << endl << "Iterative: " << endl;
for (int iteration = 0; iteration < iterations; iteration++) {
if (iteration != 0) {
cout << ", ";
}
cout << fibonacciIterative(iteration);
}
cout << endl;
cout << endl << "Recursive: " << endl;
for (int iteration = 0; iteration < iterations; iteration++) {
if (iteration != 0) {
cout << ", ";
}
cout << fibonacciRecursive(iteration);
}
cout << endl;
//
// Let the user know we're done.
//
printHeader();
cout << endl << "Done With Whiteboard Coding." << endl;
}