Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
272 lines (203 sloc) 10.2 KB
I'm adding this to Github because maybe people want to add their own implementations
of these algorithms and data structures. It can also provide a useful reference.
I'm currently working on an executable binary that runs these algorithms from the
command line. If you navigate to the c/ directory, you can build the binary with make.
If it was built without errors, you can check out the help info:
$ ./algorithms help
to see what algorithms are currently available. To get more info on a specific algorithm:
$ ./algorithms help <algo>
Each algorithm can either read from the command line (argv) or from stdin. This means
you can pipe data into the algorithms or just run them standalone.
Example for running the string reverse algorithm from the command line:
$ ./algorithms strrev hello world
dlrow olleh
And from stdin:
$ echo hello world | ./algorithms strrev
dlrow olleh
(c/find_int_pairs/main.c) Find integer pairs in an array that sum up to a specific number N.
(c/mid.c) Find the weighted midpoint of an array by the sum of two subsequences.
- split the array into two subsequences that have equal (or near-equal) sums
and print the midpoint value.
Given an array of integers and another integer, determine if the array of integers
can be manipulated into equaling the second integer using standard math operations.
- similar to subset sum problem
(c/strrev.c) Reverse a string in-place. "hello world" -> "dlrow olleh"
(c/strrev.c) Reverse a string's words in-place. "hello world" -> "world hello"
(c/lzero.c) Partition an array in such a way that zeroes are moved to the left side of the array
and all other numbers to the right side of the array. In-place only.
Given a string, remove all characters from the string that are present in another
string called 'filter'.
- Does this mean if any character is in filter it needs to be removed from source string?
- Or does it mean to remove substrings that match filter?
Write an algorithm to detect the largest palindrome in a string.
Find intersection of two arrays.
- sorted and unsorted (separate problems)
- What if one array is larger than the other?
Find a number in an array that's repeated an odd number of times.
How would you find duplicates in two unsorted integer arrays?
Find second largest number in an array. Find the n-th largest number in an array.
Find a pythagorean triplet from an array
Write a function to count the number of words in a string.
(c/power.c) Compute a^n for positive integer exponents.
- basic idea is to keep multiplying a by itself n times.
f(n) = a^n = { f(n-1) * a if n > 1
a if n = 1 }
(c/power_of_two.c) Write an algorithm to determine if an integer N is a power of 2.
- N = 8 is a power of 2 (2^3)
- log2n (binary logarithm) will do this. log2(8) == 3, pow(2, 3) == 8
(c/power_of_two.c) Write an algorithm to determine if an integer X is a power of Y.
- X = Y^n
- same idea as binary logarithm: logY(X) == n, pow(Y, n) == X
(c/mergesort.c) Write mergesort, quicksort, heapsort, etc. on-the-fly.
Write a function to merge two sorted arrays.
(c/permute.c) Write an algorithm to generate all permutations of a string.
Each key on a phone represents a set of letters. Given a phone number,
write a program to output all the possible strings the phone number represents.
(c/queens.c) Write a backtracking algorithm to solve the n-queens problem.
n-queens problem: place n-queens in a nxn chess board so that they
can't attack eachother. Solution for 4-queens problem is below:
[ ] [X] [ ] [ ]
[ ] [ ] [ ] [X]
[X] [ ] [ ] [ ]
[ ] [ ] [X] [ ]
Write code to find the second shortest path between two given nodes in an undirected graph.
Print all the shortest paths in a grid (Cartesian), given the starting point and ending point.
Given a set of shapes in 2D space and a coordinate pair, write a function
that returns true if any of the shapes overlap the coordinate pair.
Graph coloring problem
- assign the smallest number of colors to vertices of a graph
so that no two adjacent vertices are the same color.
1. Give the size of the following struct:
struct {
char a, b;
int c;
- 8 bytes: 1 byte for each of a and b, 4 bytes for c, but the compiler
takes alignment into account, so the 6 required bytes are aligned a word boundary
at 8 bytes.
- system-dependent of course.
- fetching an aligned word is faster on most processors.
2. (c/data_structures/linked_list.c) Reverse a singly-linked list
3. Given a binary tree, print the nodes at each level.
- breadth-first search
- using a queue instead of a stack
- write a function to determine the height of the binary tree
- it's a very simple function that calculates the number of edges between nodes.
if (!p)
return -1; // path from root to leaf is N-1, where N is number of nodes in path
return MAX(height(p->left), height(p->right)) + 1;
4. Graph theory; Write a function to check if a graph is bipartite (a bigraph).
5. Given two binary trees, write a function that returns true if their elements are
the same, irrespective of the trees' structures.
6. Implement a stack using a queue.
- Facebook
7. Hash tables.
- How to avoid collision in a hash table?
- keep a linked list for each key in the table so collisions are just added to the
- remove stale values when another value maps to the same key.
- Some problems with hash tables?
- choosing an effective hash function can be difficult.
- not effective on small number of entries. it's faster to do sequential lookups.
- hash table entries are distributed in memory, so it's possible to have a microprocessor
cache miss.
8. Implement a BST.
- how do you check if a binary tree is a binary search tree?
9. Write a function that outputs the contents of a binary tree to a text file.
- write a function to parse that file and generate a binary tree from it.
10. Determine the intersection point of two linked lists.
11. (c/data_structures/binary_tree.c) Given a reference to a node in a BST, find its successor.
- the successor of a node x is the node with the smallest key greater than key[x].
12. find the nth fibonacci number.
- f(n) = { f(n-1) + f(n-2) if n > 1,
1 if n == 1
0 if n == 0 }
1. Given two BSTs, write a function which tells if the two trees are the same.
2. Reverse the bits in a 32-bit integer.
3. Write a function to validate a BST.
4. Find the common numbers in two text files.
5. Set theory (union, intersection, etc).
6. Find the median of a large set of numbers distributed over several machines in a network.
7. Given a tree and a leaf node, write code to make the leaf node the root of the tree.
8. Given a "window" of size k and an array of size N, find the minimum of each "window"
as it slides through the array.
- not sure what the goal is with this problem...
9. (c/array_manipulation/spiral_print.c)
Given a matrix, print it clockwise from the first element to the very inner
- "spiral print a 2D array"
- Are the dimensions known / passed as arguments?
[ ]->[ ]->[ ]
[ ]->[ ] [ ]
^ |
| v
[ ]<-[ ]<-[ ]
10. implement a priority queue
- insert, pop, update_priority
11. traveling salesman problem.
1. multiply two very large numbers given as strings.
- return the product as a string.
2. Write a function to calculate the square root of a number.
3. Given a binary tree, write a function to find the length of the longest path in the tree.
- find minimum depth of BST
4. Write binary search.
5. Given an array of integers and a size, find 3 integers that sum to zero.
- explain brute force algorithm
- optimize the algorithm
6. A file contains 10 billion strings. You have N machines available. Find duplicate strings.
7. Given a set of integers, print out all its subsets.
8. Print out all permutations of k numbers out of 1...N
- k = 2, N = 4, {12, 13, 14, 23, 24, 34}
9. What's the complexity of binary search?
10. Given the numbers 1 to 1000, what is the minimum numbers guesses needed to find a
specific number if you are given the hint "higher" or "lower" for each guess you make?
- binary search
11. Given a large string, find a substring in it.
12. Given a list of strings, find if each string has an anagram in the list.
13. Given two events, each with a start and end time, write a function that
returns true if the events overlap.
14. Print a binary tree in infix order. Recursive and iterative.
15. Find the n-th smallest element in a binary tree.
- write a function to take a BST and a value k as input and have
it print the k-th smallest element in the BST.
16. Write a function that takes a binary tree as input and have it perform in order
traversal - recursive and iterative.
17. Print a binary search tree. Each level on a new line.
Misc. Technical Questions
1. How does an ethernet switch route?
2. How would you sort one million integers?
- "external sorting"
- divide and conquer across multiple machines for large inputs.
3. Describe the TCP handshake.
4. How can multithreading help to make a process faster?
5. A file contains 10 billion strings. You have N number of systems available.
How do you find duplicate strings?
6. What is the running time of quicksort?
7. What's the complexity of binary search?