Data Structure is the most important aspect in interviews. Most questions are on array, string, linked list, tree, stack, queue.
Array and string are two basic data structure. They are both continuous memory. Linked list and tree have high frequency occurrence in interviews. Since pointer is used very often, robust should be taken care of. Stack and recursion always stay together.
Array
Space Efficiency is bad, some spare room is not efficiently used.
O(1) read/write any element. Time Efficiency is good. We can make a simple hash table by setting the index of array to key. The value of every element become value of hash table.
Vector is a dynamic array in C++ STL. Vector extend its volume by getting double space, putting the old data to new vector and releasing the previous memory, Which is bad to time efficiency. We try our best to reduce times of change of volume.
When array is used as a parameter, then it becomes a pointer whose size is 4 bytes on 32 bits OS.
Initialization:
one dimension
int foo [5] = { 16, 2, 77, 40, 12071 }; int bar [5] = { 10, 20, 30 }; int baz [5] = { }; int foo [] = { 16, 2, 77, 40, 12071 }; int foo[] = { 10, 20, 30 }; int foo[] { 10, 20, 30 };
Multidimensional Arrays
int jimmy [3][5]; int array[3][5] = { { 1, 2, 3, 4, 5, }, // row 0 { 6, 7, 8, 9, 10, }, // row 1 { 11, 12, 13, 14, 15 } // row 2 }; int array[][5] = { { 1, 2, 3, 4, 5, }, { 6, 7, 8, 9, 10, }, { 11, 12, 13, 14, 15 } }; int array[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; int array[3][5] = { 0 };
space replacement
int replaceBlank(char string[], int length) { if (string == NULL || length <= 0) return 0; int spaceCount = 0; int i = 0; while (char c=string[i]){ if (c == ‘ ‘) spaceCount += 2; i++; } int lastIdx = length + spaceCount - 1; for (int i = length - 1; i >= 0; i--){ if (string[i] == ‘ ‘){ string[lastIdx--] = ‘0‘; string[lastIdx--] = ‘2‘; string[lastIdx--] = ‘%‘; } else{ string[lastIdx--] = string[i]; } } return length + spaceCount; }