Saturday, October 31, 2015

Day 132, #287 #290 #291 #292 #296 Find the Duplicate Number, Word Pattern, Word Pattern II, Nim Game, Best Meeting Point

Find the Duplicate Number
 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:


  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.
-----------------------------------------------------------------------
Sol #1: 因为数字的范围是1 - n, 在范围内取一个值,遍历所给的数组,记下所有比这个值小的个数,进行对比。取值的方法用binary search
O(nlgn)
class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int n = nums.size() - 1;
        int low = 1, high = n;
        
        while (low < high) {
            int mid = (low + high) / 2;
            int count = 0;
            for (int i = 0; i <= n; i++) {
                if (nums[i] <= mid) {
                    count++;
                }
            }
            
            if (count > mid) {
                high = mid;
            }else {
                low = mid + 1;
            }
            if (low == high) return low;
        }
        
    }
};
O(n)
ref:  http://keithschwarz.com/interesting/code/?dir=find-duplicate
class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int n = nums.size();
        int slow = n, fast = n;
        
        while (true) {
            fast = nums[nums[fast - 1] - 1];
            slow = nums[slow - 1];
            if (slow == fast) break;
        }
        
        slow = n;
        while (slow != fast) {
            slow = nums[slow - 1];
            fast = nums[fast - 1];
        }
        
        return slow;
    }
};

Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
--------------------------------------------------
2个hashmap 互相对应
class Solution {
public:
    string getWord(string str, int &index) {
        string rt = "";
        for (; index < str.length(); index++) {
            if (isalpha(str[index])) {
                rt += str[index];
            }else break;
        }
        index++;
        return rt;
    }

    bool wordPattern(string pattern, string str) {
        unordered_map<char,string> pToS;
        unordered_map<string,char> sToP;
        int index = 0;
        
        for (int i = 0; i < pattern.length(); i++) {
            if (index == str.length()) return false;
            string word = getWord(str,index);
            if (pToS.find(pattern[i]) == pToS.end()) {
                pToS[pattern[i]] = word;
            }else if (word != pToS[pattern[i]]) return false;
            
            if (sToP.find(word) == sToP.end()) {
                sToP[word] = pattern[i];
            }else if (sToP[word] != pattern[i]) return false;
        }
        
        return index == str.length() + 1;
    }
};

Word Pattern II
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.
Examples:
  1. pattern = "abab", str = "redblueredblue" should return true.
  2. pattern = "aaaa", str = "asdasdasdasd" should return true.
  3. pattern = "aabb", str = "xyzabcxzyabc" should return false.
Notes:
You may assume both pattern and str contains only lowercase letters.
-------------------------------------------------------
back tracking
对一个pattern[i], 试遍每一种可能
class Solution {
public:
    bool helper(string pattern, int i, string str, int is, unordered_map<char,string> &ptos, unordered_map<string,char> &stop) {
        if (i == pattern.length() && is == str.length()) return true;
        if (i == pattern.length() || is == str.length()) return false;
        
        if (ptos.find(pattern[i]) != ptos.end()) {
            if (ptos[pattern[i]] == str.substr(is,ptos[pattern[i]].length())) {
                return helper(pattern,i + 1, str, is + ptos[pattern[i]].length(),ptos,stop);
            }
            return false;
        }
        
        for (int j = is; j < str.length(); j++) {
            string word = str.substr(is,j - is + 1);
            if (stop.find(word) != stop.end()) continue;
            
            ptos[pattern[i]] = word;
            stop[word] = pattern[i];
            if (helper(pattern, i + 1, str, j + 1, ptos,stop)) return true;
            ptos.erase(pattern[i]);
            stop.erase(word);
        }
        
        return false;
    }

    bool wordPatternMatch(string pattern, string str) {
        unordered_map<char,string> ptos;
        unordered_map<string,char> stop;
        
        return helper(pattern,0,str,0,ptos,stop);
    }
};

Nim Game
 You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
Hint:
  1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner? 
---------------------------------------
可递归,可DP,但是以下为最简
class Solution {
public:
    bool canWinNim(int n) {
        return n % 4;
    }
};

Best Meeting Point
A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
For example, given three people living at (0,0)(0,4), and (2,2):
1 - 0 - 0 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0
The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimal. So return 6.
Hint:
  1. Try to solve it in one dimension first. How can this solution apply to the two dimension case?
--------------------------------------------------------------
O(m*n*log(m*n) )
class Solution {
public:
    int minTotalDistance(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<int> I,J;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    I.push_back(i);
                    J.push_back(j);
                }
            }
        }
        
        return getDis(I) + getDis(J);
    }
    
    int getDis(vector<int> &num) {
        int rt = 0, left = 0, right = num.size() - 1;
        sort(num.begin(), num.end());
        
        while (left < right) {
            rt += num[right] - num[left];
            right--;
            left++;
        }
        
        return rt;
    }
};

O(mn)
class Solution {
public:
    int minTotalDistance(vector>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector I,J;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    I.push_back(i);
                }
            }
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[j][i] == 1) {
                    J.push_back(i);
                }
            }
        }
        
        return getDis(I) + getDis(J);
    }
    
    int getDis(vector &num) {
        int rt = 0, left = 0, right = num.size() - 1;
        
        while (left < right) {
            rt += num[right] - num[left];
            right--;
            left++;
        }
        
        return rt;
    }
};

Tuesday, October 27, 2015

Day 131, #288 #289 #293 #294 #295 #298 Unique Word Abbreviation, Game of Life, Flip Game, Flip Game II, Find Median from Data Stream, Binary Tree Longest Consecutive Sequence

Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:
a) it                      --> it    (no abbreviation)

     1
b) d|o|g                   --> d1g

              1    1  1
     1---5----0----5--8
c) i|nternationalizatio|n  --> i18n

              1
     1---5----0
d) l|ocalizatio|n          --> l10n
Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if no other word from the dictionary has the same abbreviation.
Example: 
Given dictionary = [ "deer", "door", "cake", "card" ]

isUnique("dear") -> false
isUnique("cart") -> true
isUnique("cane") -> false
isUnique("make") -> true
-----------------------------------------------------------------
COME_BACK
看清题意
class ValidWordAbbr {
public:
    string getAbre(string s) {
        int len = s.length();
        if (len > 1) {
            s = s[0] + to_string(len - 2) + s[len - 1];
        }
        return s;
    }
    
    ValidWordAbbr(vector<string> &dictionary) {
        for (int i = 0; i < dictionary.size(); i++) {
            string abre = getAbre(dictionary[i]);
            if (dic.find(abre) == dic.end()) {
                vector<string> second;
                second.push_back(dictionary[i]);
                dic[abre] = second;
            }else {
                dic[abre].push_back(dictionary[i]);
            }
        }
    }

    bool isUnique(string word) {
        string abre = getAbre(word);
        if (dic.find(abre) == dic.end()) return true;
        return dic[abre][0] == word && dic[abre].size() == 1;
    }
    
private:
    unordered_map<string,vector<string>> dic;
};


// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa(dictionary);
// vwa.isUnique("hello");
// vwa.isUnique("anotherWord");

Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.
Follow up
  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
---------------------------------------------------------------------
额外空间
class Solution {
public:
    vector<int> row;
    vector<int> col; 
    bool isLive(int i, int j, int m, int n, vector<vector<int>>& board) {
        if (i < 0 || j < 0 || i == m || j == n) return false;
        return board[i][j];
    }

    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        int n = board[0].size();
        vector<vector<int>> rt(m, vector<int>(n));
        row = {-1,-1,-1,0,0,1,1,1};
        col = {-1,0,1,-1,1,-1,0,1};
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int numOfLives = 0;
                for (int k = 0; k < 8; k++) {
                    if (isLive(row[k] + i,col[k] + j, m, n, board)) {
                        numOfLives++;
                    }
                }
                
                if (numOfLives < 2 || numOfLives > 3) rt[i][j] = 0;
                else if (board[i][j] && numOfLives <= 3) rt[i][j] = 1;
                else if (numOfLives == 3) rt[i][j] = 1;
            }
        }
        board = rt;
    }
};

constant space
用-1来表示之前为0,现在为1. -2来表示之前为1,现在为0
0 -> -1 -> 1
1 -> -2 -> 0
class Solution {
public:
    vector<int> row;
    vector<int> col; 
    bool isLive(int i, int j, int m, int n, vector<vector<int>>& board) {
        if (i < 0 || j < 0 || i == m || j == n) return false;
        if (board[i][j] == 0 || board[i][j] == -1) return false; 
        return true;
    }

    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        int n = board[0].size();
        row = {-1,-1,-1,0,0,1,1,1};
        col = {-1,0,1,-1,1,-1,0,1};
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int numOfLives = 0;
                for (int k = 0; k < 8; k++) {
                    if (isLive(row[k] + i,col[k] + j, m, n, board)) {
                        numOfLives++;
                    }
                }
                
                if (board[i][j] && numOfLives < 2) board[i][j] = -2;
                else if (board[i][j] && (numOfLives == 3 || numOfLives == 2)) continue;
                else if (board[i][j] && numOfLives > 3) board[i][j] = -2;
                else if (numOfLives == 3) board[i][j] = -1;
            }
        }
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == -1) board[i][j] = 1;
                if (board[i][j] == -2) board[i][j] = 0;
            }
        }
    }
};

Flip Game
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to compute all possible states of the string after one valid move.
For example, given s = "++++", after one move, it may become one of the following states:
[
  "--++",
  "+--+",
  "++--"
]
If there is no valid move, return an empty list [].
---------------------------------------------
class Solution {
public:
    vector<string> generatePossibleNextMoves(string s) {
        vector<string> rt;
        for (int i = 1; i < s.length(); i++) {
            if (s[i] == '+' && s[i - 1] == '+') {
                s[i] = '-';
                s[i - 1] = '-';
                rt.push_back(s);
                s[i] = '+';
                s[i - 1] = '+';
            }
        }
        
        return rt;
    }
};

Flip Game II
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to determine if the starting player can guarantee a win.
For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".
Follow up:
Derive your algorithm's runtime complexity.
--------------------------------------------------------------
递归
T(n) = n * T(n - 1)
        = n * (n - 1) * T(n - 2)
        = n * (n - 1) * (n - 2) * T(n - 3)
        ...
        = n!
class Solution {
public:
    bool canWin(string s) {
        for (int i = 1; i < s.length(); i++) {
            if (s[i] == '+' && s[i - 1] == '+') {
                s[i] = '-';
                s[i - 1] = '-';
                if (!canWin(s)) return true;
                s[i] = '+';
                s[i - 1] = '+';
            }
        }
        
        return false;
    }
};
COME_BACK
game theory的方法可以达到O(n^2)

Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples: 
[2,3,4] , the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
  • void addNum(int num) - Add a integer number from the data stream to the data structure.
  • double findMedian() - Return the median of all elements so far.
For example:
add(1)
add(2)
findMedian() -> 1.5
add(3) 
findMedian() -> 2
---------------------------------------------------------------------------
2个queue,还有其他方法吗
一个TreeSet加2个指针,跟2个heap原理是一样的
class MedianFinder {
public:
    struct cmp {
        bool operator() (int i1, int i2) {
            return i1 > i2;
        }
    };
    
    // Adds a number into the data structure.
    void addNum(int num) {
        small.push(num);
        int temp = small.top();
        small.pop();
        large.push(temp);
        temp = large.top();
        large.pop();
        if (small.size() > large.size()) {
            large.push(temp);
        }else {
            small.push(temp);
        }
    }

    // Returns the median of current data stream
    double findMedian() {
        if ((small.size() + large.size()) % 2 == 0) {
            return (small.top() + large.top()) / 2.0;
        }else {
            return small.top();
        }
    }

private:
    priority_queue<int> small;
    priority_queue<int,vector<int>,cmp> large;
};

// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();

Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path.
The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).
For example,
   1
    \
     3
    / \
   2   4
        \
         5
Longest consecutive sequence path is 3-4-5, so return 3.
   2
    \
     3
    / 
   2    
  / 
 1
Longest consecutive sequence path is 2-3,not3-2-1, so return 2.
-------------------------------------------------------
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void dfs(TreeNode* root, int cur, int &maxL, int target) {
        if (root == NULL) return;
        if (root->val == target) {
            cur++;
        }else {
            cur = 1;
        }
        maxL = max(maxL,cur);
        dfs(root->left, cur, maxL, root->val + 1);
        dfs(root->right, cur, maxL, root->val + 1);
    }

    int longestConsecutive(TreeNode* root) {
        int maxL = 0;
        if (root == NULL) return 0;
        dfs(root,0,maxL,root->val);
        return maxL;
    }
};

Friday, October 16, 2015

Google interview questions #8

refhttp://www.mitbbs.com/article_t/JobHunting/33072599.html

是烙印, 问的是LCA of DAG. 全程不让写代码, 就一直问follow up, 问复杂度, 问怎
么做, 为什么要这么做, 用那个为什么不好, 后来问要是有很多pair of nodes 求LCA
要怎么做preprocessing.

解法
2个node各做bfs,记录走过的node和距离,如果出现重复就记录2个的距离和,直到找到最小的距离和

follow up:
预先计算出每个node到其他所有node的距离。当给定2个node时,找出所有的共同点,计算距离和

--------------------------------------------------------------------------
refhttp://www.mitbbs.com/article_t/JobHunting/33073385.html

x_0 = C (integer)
if x_n is even then x_{n+1} is x_n / 2
if x_n is odd then x_{n+1} to be 3 * x_n + 1

这题的复杂度是O(logC)吗?

https://en.wikipedia.org/wiki/Collatz_conjecture

---------------------------------------------------------------------
ref: http://www.meetqun.com/forum.php?mod=viewthread&tid=11377&extra=page%3D1%26filter%3Dtypeid%26typeid%3D50

一队人,每一个人有两个值(height,tvalue),其中height代表身高,tvalue代表这个人前面有多少人比他高。现在把这个array of (height, tvalue)打乱,然后要还原这个数列。.

解法
按height排序,从最低height开始,tvalue就是它在新array里的最终位置。需要注意是,每次都要对之前index前面里已经有多少被放置

最终的index = tvalue + (index之前已经确定的元素个数)

所以复杂度是O(n ^ 2)

nlogn 的解法:根据array的index建立一个bst,附带一个额外的值代表此subtree当前有多少个node被填过

-------------------------------------------------------------------------------------
refhttp://www.meetqun.com/forum.php?mod=viewthread&tid=7849&extra=page%3D1%26filter%3Dtypeid%26typeid%3D50

#1 把bst变成双向链表
解法:inorder遍历,保证一个pointer pass by reference

逆序数
解法: divide and conquer(merge sort)

#2 实现Candy Crush
1.m行n列的矩阵;2.q种花色物品;3.矩阵里每个cell的花色都是random的;4.初始化之后至少一个available的move;5.no 3-run,即初始化的时候不能直接出现三个同种物品连一线的情况。
好像还有其他几个要求,我已经记不清了。
我就开始按照他的要求挨个进行分析。先是random函数,然后就可以用双层for循环来填充每一个cell。我说但是一个available的move这个太难了,因为有太多的情况。就问他要提示,而且我不断做尝试。他说“其实答案比你想想的简单多了。最后直接告诉我:“你可以直接在填充所有的cell之前先创造一个可以move的三个物品先放进去。我恍然大悟,对对对,在双层for循环之前先做这一步操作。
下一个是"no 3-run的条件”。我说就每当在填充一个之前先往上下左右四个方向的两个格子都跟将random生成的物品进行比对。他说:“其实另一种情况。”我说:“就是将要填充的物品正好在两个同色物品之间。”他说对。我说咱们就可以用一个while循环,如果random生成的颜色不符合要求(设置一个boolean函数来判断,这里不要求实现)就继续循环。; u# V. Y: \ p7 B$ {/ a/ [: n
他说:“OK。其实这样会stuck到一种情况,你来想想。”我就想到比如比如当前方格的上、下、左、右各有一种两个同色物品排列,那么这个cell填这四种颜色的任何一种都不对了。”他说对,怎么避免呢?经过他的提示我就写这样的情况下直接清除board中所有已填颜色从头来过。0 L% F- Z) k( F
难点解决了,下一步就是写代码了,然后照相。
感觉这种题目面试官已经在里面设置好了处理的难点来等待你遇到和解决,然后看你尝试解决问题的方法以及交流能力。就像寻宝一样不断地试探、前进、解决问题。
#3 Tries

#4
比如1729 = 1^3 + 12 ^ 3 = 9 ^ 3 + 10 ^ 3。像这种可以由两对或两对以上的立方和组成的数叫做R某某某数(单词记不清了)。让你构造一个函数,输入是N,输出小于N的所有这种数。/ u" f% G( Z( |9 Q0 f- _1 H5 K
其实方法很简单,就是将从1到N的立方根之间的数,从中任取两个来求立方和,然后看结果集当中有没有出现两次或两次以上的数。就是类似于暴力了。

解法: 暴力 + hash,从1开始算到N

----------------------------------------------------------
ref: http://www.meetqun.com/forum.php?mod=viewthread&tid=11067&extra=page%3D1%26filter%3Dtypeid%26typeid%3D50

Google onsite
1. 类似这道题:
给如下的数据格式:<start_time, end_time, value>
For example,
1, 3, 100
2, 4, 200
5, 6, 300
。。。
这些数据时间点可能有重合。在时间段2~3之间,value的和是100+200 = 300. 找出这
组数据中最高的value和
[consider end points]

解法:类似meeting room,把开始、结束时间排序,设2个指针p1, p2。
如果是开始时间,cur + value[p1++], 结束时间cur - value[p2++]

2.find k most frequent words from a file
解法:priority queue + hash map

3.brainstorming: 一个上传文件的service,之前正常运转,突然有一天挂了,这期间
没改代码。问怎么排查问题。

查log,物理硬件

-------------------------------------------------------------------------------
ref: http://www.meetqun.com/forum.php?mod=viewthread&tid=301&extra=page%3D1%26filter%3Dtypeid%26typeid%3D50&page=6

check一个数是否是3的次幂


遍历
查表,32位内3的幂也就几十位
2分

---------------------------------------------------------------------------------
refhttp://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/
word wrap

解法:
标准dp
 int cube(int num) {
 return  num * num * num;
}

int minCost(vector<int> &v,vector<int> &dp,int index, int width) {
 if (index == v.size()) return 0;
 if (dp[index] != INT_MAX) return dp[index];
 int len = v[index];
 int minC = INT_MAX;
 for (int i = index; i < v.size(); i++) {
  minC = min(minC, cube(width - len) + minCost(v,dp,i + 1,width));
  len += v[i] + 1;
  if (width < len) break;
 }
 dp[index] = minC;
 return minC;
}

int spawn(vector<int> &v, int width) {
 vector<int> dp(v.size(),INT_MAX);
 return minCost(v,dp,0,width);
}

---------------------------------------
round 1: 给定一个class
class Event {
int id; /nsor id
int timestamp;
boolean status;
}
意思是某个sensor会触发一个事件使其状态status改变,true->false or false->true. 设计数据结构,存储以下信息:每个事件的sensor id, timestamp和status,使其可以响应一种query, query包含id, timestamp,返回这个id在这个时刻的status
follow up: 如果来的event不是按照timestamp递增的,怎么修改数据结构

round 2:
一个二维矩阵,里面是bits (1 or 0),可以用int 或byte表示,左右翻转这个矩阵,有什么优化

round 3:
一个猜词游戏,给定一个词典,里面的词的长度都一样,有一个secret word在这个词典里,每次从词典里选一个词猜,只会返回和secret word有几个字母是一样的(只知道数量,不知道位置)。问怎样猜能尽量减少猜测次数

Saturday, October 3, 2015

Palantir interview question #1

--------------------------------------------------------------------
ref https://shawnlincoding.wordpress.com/2015/03/16/palantir%E9%9D%A2%E7%BB%8F/
1,
1. 给你一个棋盘 int[][] board, 你可以交换任何一个棋子和它的邻居(横向或者竖向相邻的棋子),如果交换后,在横向或者竖向产生了大于等于三个连续的一样的棋子 e.g. 4 4 4
5
5
5
那么就算交换有效。(就是一个比较常见的游戏,忘记叫啥名字了)
请你写一个函数返回所有可以有效交换的棋子的坐标对。 比如 ((0, 0), (1, 0)) , ((3,2),(3,3)).

2,看code,debug, 然后写出正确的code,这个没啥说的

3 Merge interval
Running Median : follow up: O(1) space


--------------------------------------------------------------------------------------

write a fuction to titlecase a string
for example
input: the quick brown fox
output: The Quick Brown Fox
two pointers, remember to update flag


DFS,很像coursera普林算法课讲得CC, 看看所以0是不是在一个CC里面
从0,0开始做一次DFS,如果有0没被marked到那就是invalid的