Tuesday, November 29, 2011

ThoughtWorks Question: Game of Life

Problem: 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:
  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 method to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

Example (Taken from leetcode):

Input: 
[
  [0,1,0],
  [0,0,1],
  [1,1,1],
  [0,0,0]
]
Output: 
[
  [0,0,0],
  [1,0,1],
  [0,1,1],
  [0,1,0]
]


Approach: It can be easily done if we use O(m x n) space, basically copy the input matrix to another matrix of same size and then according to the new matrix, we'll update the original matrix. The problem is we want to avoid the extra space. 

The solution is still very simple. Right now a cell is live when the value is 1 and dead if value is 0. We can use some values say x and y other than these values to denote if a particular cell died (x) or got a life (y) just because of latest iteration.

I am gonna take -1 as x i.e. a cell is recently died. I am taking -1 as it just help while comparing for the live neighbors as what I can say if Abs(board[i][j]) == 1 then neighbor cell at [i][j] is live for this particular iteration. I am gonna take 2 as y i.e. a cell is recently got a life. There is no logic for this, I just chose a positive value to denote live cells.

Now you can see we need two loops here. In the first loop we will assign -1 and 2 according to the given rules if required and in the second loop we will convert -1 to 0 and 2 to 1.

Cheers!


Implementation in C#:

        public void GameOfLife(int[][] board)
        {
            if (board?.Length <= 0 || board[0]?.Length <= 0)
            {
                return;
            }

            for (int i = 0; i < board.Length; ++i)
            {
                for (int j = 0; j < board[0].Length; ++j)
                {
                    bool modified = this.ValidateAndApplyRule1IfNeeded(board, i, j) ||
                    this.ValidateAndApplyRule2IfNeeded(board, i, j) ||
                    this.ValidateAndApplyRule3IfNeeded(board, i, j) ||
                    this.ValidateAndApplyRule4IfNeeded(board, i, j);

                    // Just for debugging purpose
                    // Console.WriteLine($"Cell [{i}][{j}] is modified: {modified}");
                }
            }

            for (int i = 0; i < board.Length; ++i)
            {
                for (int j = 0; j < board[0].Length; ++j)
                {
                    board[i][j] = board[i][j] <= 0 ? 0 : 1;
                }
            }
        }

        private bool ValidateAndApplyRule1IfNeeded(int[][] board, int i, int j)
        {
            if (board[i][j] == 1)
            {
                if (this.CountLiveNeighbors(board, i, j) < 2)
                {
                    board[i][j] = -1;
                    return true;
                }
            }

            return false;
        }

        private bool ValidateAndApplyRule2IfNeeded(int[][] board, int i, int j)
        {
            if (board[i][j] == 1)
            {
                int liveNeighbors = this.CountLiveNeighbors(board, i, j);
                if (liveNeighbors == 2 || liveNeighbors == 3)
                {
                    return true;
                }
            }

            return false;
        }

        private bool ValidateAndApplyRule3IfNeeded(int[][] board, int i, int j)
        {
            if (board[i][j] == 1)
            {
                if (this.CountLiveNeighbors(board, i, j) > 3)
                {
                    board[i][j] = -1;
                    return true;
                }
            }

            return false;
        }

        private bool ValidateAndApplyRule4IfNeeded(int[][] board, int i, int j)
        {
            if (board[i][j] == 0)
            {
                if (this.CountLiveNeighbors(board, i, j) == 3)
                {
                    board[i][j] = 2;
                    return true;
                }
            }

            return false;
        }

        private int CountLiveNeighbors(int[][] board, int i, int j)
        {
            int liveNeighbors = 0;
            if (i > 0)
            {
                liveNeighbors += Math.Abs(board[i - 1][j]) == 1 ? 1 : 0;
            }
            if (j > 0)
            {
                liveNeighbors += Math.Abs(board[i][j - 1]) == 1 ? 1 : 0;
            }
            if (i > 0 && j > 0)
            {
                liveNeighbors += Math.Abs(board[i - 1][j - 1]) == 1 ? 1 : 0;
            }
            if (i < board.Length - 1)
            {
                liveNeighbors += Math.Abs(board[i + 1][j]) == 1 ? 1 : 0;
            }
            if (j < board[0].Length - 1)
            {
                liveNeighbors += Math.Abs(board[i][j + 1]) == 1 ? 1 : 0;
            }
            if (i < board.Length - 1 && j < board[0].Length - 1)
            {
                liveNeighbors += Math.Abs(board[i + 1][j + 1]) == 1 ? 1 : 0;
            }
            if (i < board.Length - 1 && j > 0)
            {
                liveNeighbors += Math.Abs(board[i + 1][j - 1]) == 1 ? 1 : 0;
            }
            if (i > 0 && j < board[0].Length - 1)
            {
                liveNeighbors += Math.Abs(board[i - 1][j + 1]) == 1 ? 1 : 0;
            }

            return liveNeighbors;
        }


Complexity: O(mxn)

Tuesday, November 1, 2011

Infibeam Question: Reverse Level Order Traversal of Binary Tree

void BSTree::reverseLevelOrderTraversal(Node* node)
{
    queue<Node*> qu;
    stack<int> st;
    qu.push(node);
    while(!qu.empty())
    {
        Node* temp = qu.front();
        st.push(temp->data);
        if(temp->right)
            qu.push(temp->right);
        if(temp->left)
            qu.push(temp->left);
        qu.pop();
    }
    while(!st.empty())
    {
        cout<<st.top()<<'\t';
        st.pop();
    }
}

Infibeam Question: Implement T9 Dictionary

Solution is to take hash with key is the number and the value is the list of words which can be made by pressing the digits in the number. For example 4663 --> good, home

LoadWords Operation --

   1. Read words and their popularity one by one from file.
   2. Get the number corresponding to the word.
   3. Make an object which contains the word and its popularity.
   4. hash[number].insert(wordObject).

DisplayWords Operation --

It was given that most popular 5 words need to be displayed.
  1. Maintain a heap based on popularity of size 5.
  2. Maintain the keys in the hash in string sorted order. ( Map can be used for hash)
  3. Look for the keys which are started with the given number.

Source Code --

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<fstream>
#include<algorithm>

using namespace std;

//Class word
class Word
{
public:
    Word(string w="", int p=0);
    string getWord();
    int getPopularity();
private:
    string word;     //actual word
    int popularity;  //popularity of word
};

Word::Word(string w, int p):word(w), popularity(p)
{
}

string Word::getWord()
{
    return word;
}

int Word::getPopularity()
{
    return popularity;
}

//Comparator function used to sort the vector of Word with key popularity of the word
bool wordCompare(Word a, Word b)
{
    return a.getPopularity() == b.getPopularity() && a.getWord() < b.getWord() || 
    a.getPopularity() > b.getPopularity();
}

//This is helper class to T9Dictionary class. It uses heap to maintain the top 5 i.e. total words to display
class T9DictDisplayHelperHeap
{
public:
    T9DictDisplayHelperHeap(int num = 5);  //num is number of words to display
    void pushHeap(Word word);   //insert word into heap
    void displayWords();        //display the words
private:
    int count;
    vector<Word> vectWord;        //heap
    const short wordsToDisplay;
};

T9DictDisplayHelperHeap::T9DictDisplayHelperHeap(int num):wordsToDisplay(num), count(0)
{
    vectWord.reserve(wordsToDisplay);
}

void T9DictDisplayHelperHeap::pushHeap(Word word)
{
    if(count<wordsToDisplay)
    {
        vectWord.push_back(word);
        count++;
    }
    else if(count == wordsToDisplay)
    {
        make_heap(vectWord.begin(), vectWord.end(), wordCompare);
        if(vectWord.front().getPopularity() < word.getPopularity())
        {
            pop_heap(vectWord.begin(), vectWord.end(), wordCompare);
            vectWord.pop_back();
            vectWord.push_back(word);
            push_heap(vectWord.begin(), vectWord.end(), wordCompare);
        }
        count++;
    }
    else
    {
        if(vectWord.front().getPopularity() < word.getPopularity())
        {
            pop_heap(vectWord.begin(), vectWord.end(), wordCompare);
            vectWord.pop_back();
            vectWord.push_back(word);
            push_heap(vectWord.begin(), vectWord.end(), wordCompare);
        }
    }
}

void T9DictDisplayHelperHeap::displayWords()
{
    if(count < wordsToDisplay)
        sort(vectWord.begin(), vectWord.end(), wordCompare);
    else
    {
        sort_heap(vectWord.begin(), vectWord.end(), wordCompare);
    }
    int size = vectWord.size();
    for(int i = 0; i < size; ++i)
        cout<<vectWord[i].getWord()<<" : "<<vectWord[i].getPopularity()<<'\n';
}

//Dictionary Class. It is using map in which key is the number and the value is vector of corresponding words
class T9Dict
{
public:
    T9Dict(int count = 5);   //count is number of words to display
    bool loadWords(string fileName);   // load words from a file, fileName is the path of the file which contains words to be inserted in dictionary
    void displayWords(string num);       
private:
    const short wordsToDisplay;
    void addWord(string key, Word w);
    map<string, vector<Word> > mapWords;
};

T9Dict::T9Dict(int count):wordsToDisplay(count)
{
}

// For each alphabet it is taking the corresponding number like for a,b,c corresponding number is 2
// For space ' ' number is 0
// For digits number is same as the digit given
// For all other special charcters number is 1

bool T9Dict::loadWords(string fileName)
{
    ifstream in(fileName.c_str());
    if(!in)
    {
        cout<<"File: "<<fileName<<" does not exist or not accessible\n";
        return false;
    }
    int popularity = 0;
    char *temp = new char[256];
    string word;
    while(!in.eof())
    {
        string key = "";
        in>>popularity;
        //in>>word;
        in.getline(temp, 255);
        word.clear();
        word = string(temp);
        word = word.substr(1);
        int len = word.length();
        for(int i=0; i<len; ++i)
        {
            word[i] = tolower(word[i]);
            if(word[i] == ' ')
                key += '0';
            else if(word[i] >= '0' && word[i] <= '9')
            {
                key += word[i];
            }
            else if(word[i] >= 'a' && word[i] <= 'o')
            {
                key += (((word[i] - 'a') / 3) + 2 + '0') ;
            }
            else if(word[i] >= 'p' && word[i] <= 's')
            {
                key += '7';
            }
            else if(word[i] >= 't' && word[i] <= 'v')
            {
                key += '8';
            }
            else if(word[i] >= 'w' && word[i] <= 'z')
            {
                key += '9';
            }
            else
            {
                key += '1';
            }
        }
        addWord(key, Word(word, popularity));
    }

    delete temp;
    for(map<string, vector<Word> >::iterator it = mapWords.begin(); it != mapWords.end(); 
    ++it)
    {
        sort(it->second.begin(), it->second.end(), wordCompare);
    }
    return true;
}

void T9Dict::addWord(string key, Word word)
{
    mapWords[key].push_back(word);
}

void T9Dict::displayWords(string num)
{
    T9DictDisplayHelperHeap heap;
   
    map<string, vector<Word> >::iterator it = mapWords.begin();
    while(it != mapWords.end() && it->first < num)
    {
        it++;
    }
    int len = num.length();
    while(it != mapWords.end() && (it->first.substr(0, len) == num))
    {
        for(unsigned int i=0; i < it->second.size(); ++i)
            heap.pushHeap(it->second[i]);
        it++;
    }

    heap.displayWords();
}