Anda di halaman 1dari 5

afternerd.

com
P.O. Box 70686
Sunnyvale, Ca 94086

Coding Interview Cheat Sheet


If there is only one thing that you absolutely need to be comfortable with before going to your
technical interview, that is data structures & algorithms.

First: Data Structures


1- Arrays
Sequential elements that are stored in a contiguous block of memory.

Operation Big-O Complexity

Indexing O(1)

Search O(n)

Search in a sorted array O(log n)

2- Linked Lists
Linked Lists are not arrays. They are not stored in a contiguous block of memory.

Each element (node) in a linked list references the next node, and sometimes the previous node
too if it is a doubly linked list.

Stacks and Queues are two data structures that are implemented with linked lists.

A Stack is a last in, first out (LIFO) data structure whereas a Queue is a first in, first out data
structure.
Operation Big-O Complexity

Indexing O(n)

Search O(n)

Search in a sorted linked list O(n)

Insertion O(1)

3- Hash tables
These are data structures that map keys to values.

A Hash function is a mathematical function that, given an input key, returns an output that is
very likely to be unique to that key.

Hash collisions are when a hash function returns the same output for two different keys.

Operation Big-O Complexity

Indexing or Search (by key) O(1)

Insertion O(1)

4- Binary Trees
A generic binary tree is a data structure where each node in a binary tree has two children (a
left child, and a right child). All of the children are descendants of one tree node called the root
node.

A binary search tree (BST) is a special binary tree where:

o Left child has a key smaller than its parent node.


o Right child has a key greater than its parent node.

Operation Big-O Complexity

Search O(log n)

Insertion O(log n)
Second: Important Algorithms
1- Breadth First Search
BFS(initial node) {
Set all nodes to not visited;
q = new Queue();
q.enqueue(initial node);

while ( q ≠ empty ) do
{
x = q.dequeue();
if ( x has not been visited )
{
visited[x] = true;
for ( every edge (x, y))
if ( y has not been visited )
q.enqueue(y);
}
}
}

- Very important graph traversal algorithm.

- Visits the neighboring nodes first.

- Implemented with a Queue data structure.

- Search complexity is O(|E| + |V|) where E is the number of edges and V is the number of
vertices.

2- Depth First Search


DFS(u) {
u.visited = true
for each v that is adjacent to u
if v.visited == false
DFS(v)

- Another very important graph traversal algorithm.

- Visits the deepest nodes first.

- Implemented recursively or a stack.


- Search complexity is O(|E| + |V|)

3- Merge Sort
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)

Scenario Complexity
Best case O(n)
Average case O(n log n)
Worst case O(n log n)

4- Quick Sort
quickSort(arr[], low, high) {
if (low < high) {
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
partition (arr[], low, high) {
pivot = arr[high];
i = (low - 1)
for (j = low; j <= high- 1; j++) {
if (arr[j] <= pivot) {
i++;
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[high])
return (i + 1)
}

Scenario Complexity
Best case O(n)
Average case O(n log n)
Worst case O(n^2)

Third: General Advice


- Think aloud, whether you are doing a phone interview or an on-site interview
- Don’t be arrogant. I have seen many smart people getting rejected because of their
personality.
- As soon as you get a question, try to talk with the interviewer about the assumptions.
Interviewers love getting asked questions.
- Dress comfortably. No one likes coding on a white board in a suit.
- Clarify about the inputs and the data types (e.g. can the array have negative numbers?)
- Don’t jump straight to coding. Talk with the interviewer about your thought process
before you jump into coding.
- Don’t write pseudocode. Interviewers hate that.
- When you’ re coding, sometimes it is very useful to just use functions in your code and
tell the interviewer you will be implementing these functions later (especially if these
functions are performing some mundane tasks that are not crucial to the actual problem
you are trying to solve). This trick definitely puts you at ease and allows you to focus on
solving the problem at hand first.
- After you finish coding, go over your code one more time. Again, trace your code aloud
and then confidently tell your interviewer that you are done.
- Prepare questions for the interviewer about his/her role, the company, and the company
products.
- Smile and thank your interviewer after the interview.

Good luck to you in your interview J

Anda mungkin juga menyukai