Thursday, November 26, 2009

Interview Questions

1. Simulate a seven-sided die using only five-sided

2. Giving Two Strings, Find out whether they are Anagrams or not?

3. find the longest palindrome in a string?

4. find longest common substring in 2 strings using suffix tree

Some terms:
  • suffix tree of a string S: a tree whose edges are labeled with strings, such that each suffix of S corresponds to exactly one path from the tree's root to a leaf.
  • trie: each edge is labelled with a single character
  • radix tree, Patricia tree/trie: edges are labelled with a string instead of single character
suffix tree: a special type of radix tree
  • How to construct suffix tree?
  • How to find longest common substring using suffix tree?
DP algorithm runs in O(mn).

Reference:
Fast String Searching With Suffix Trees

Wednesday, November 25, 2009

Algorithm Reviews

Foundation of Algorithms Using C++ Pseudocode, 3rd Edition.
By Richard Neapolitan and Kumarss Naimipour

Chapter 5 Backtracking
Backtracking is the technique to prune non-promising nodes in the state space tree.

CLRS
Ch 11 Hash Tables

11.3 Hash Functions

11.4 Open Addressing
Uniform hashing: idealized hashing function selects a permutation with equal prob.
Linear Probing: h(k,i) = ( h'(k) + i ) mod m
Quadratic Probing:
Double Hashing

Friday, November 20, 2009

Wireless Crackers

I came across this website today:
http://forums.remote-exploit.org/

Indeed many people are constantly trying to crack other's wireless password. How scary is that?

Monday, November 16, 2009

Interview Questions

From careercup.com:
  • Given a document and a query of K words, how do u find the smallest window that covers all the words at least once in that document? (given you know the inverted lists of all K words, that is, for each word, you have a list of all its occurrrences). This one is really hard. Could someone propose an algorithm in O(n)?
Answer: Use two pointers, one is the left of the range, one is the right of the range. For each word w_i we also keep a left and right pointer. Both initialize to the head of occurrence list (0).
Initially left is at the leftest word occurrence.
(Loop)
(AdvanceRight) Advance right by advancing the leftest of the right pointers in the occurrence list.
if reached the right end, break the main loop

Keep an array of counters for each word to count how many times w_i occurs in the range.
Advance right until all words are included.
Record range(left,right)
(AdvanceLeft)
Advance the left pointer and decrement the corresponding counter whenever left pointer moves pass word w_i
Repeat until at least one word doesn't occur in the range

Record range( left-1, right) if it's the current optimal

Go back to Loop

  • Find the median of 2 sorted arrays