Monday 7 November 2011

A fresher at Stratify..

After a, I cannot believe its been so long, 7 yrs my association with my first company (Stratify Inc.), comes to an end this Wednesday.

The beauty of a first job, apart from the financial independence it provides, is the enthusiasm associated with it. I distinctly recollect, as they say, the fire in the belly, when I reported for my first day at work and guess what, I have the same feeling as I head into the next leg of my career.

I can recollect many instances where my colleague's actions/words motivated me to go the extra yard, put in that extra night out to meet our deadlines and above all the expectations the team had from me. I also recollect those long hours spent in 'mandir' which not only bonded us closer as individuals but also motivated us to walk in the next day and give our best.

After days sequenced code, smoke, code, smoke, lunch, smoke, code, smoke and then the freakishly competitive foozball we were all setup for the perfect evening  in mandir full of drinks where we all rubbed off each other's energy.. 

I was also blessed with a guardian who always motivated me and warned me when complacency or over-confidence or politics got the best of me, to him I owe more than any words I can put out here.


I believe that your first long stay at a company, and when I say long I am talking atleast 2 years, goes a long way in shaping you as a professional and what I want to take forward from Stratify is the hardwork, honesty, humility and above all the team spirit which my colleagues have instilled in me and I can only hope that my next adventure is as exciting and enriching as my first has been... So long Stratifyans.. so long.

Wednesday 2 November 2011

Sample TRIE implementation in Java

Tried my hands at implementing a TRIE data structure in Java. There are 2 classes posted here, Trie itself and TrieNode which represents a node in a Trie.

Trie class
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * TRIE data structure supporting basic dictionary operations.
 */
public class Trie {

    private final TrieNode root;

    /**
     * Creates a new empty TRIE object.
     */
    public Trie() {
        root = new TrieNode();
    }


    /**
     * Inserts the specified key into this Trie object.
     * @param key
     */
    public void insert(String key) {
        TrieNode currNode = root;
        for (char c : key.toCharArray()) {
            TrieNode child = currNode.traverse(c);
            if (child == null) {
                currNode = currNode.addEdge(c);
            } else {
                currNode = child;
            }
        }
        currNode.setKey(key);
    }

    /**
     * Returns all the keys in the Trie which start with the specified prefix.
     * @param prefix
     * @return
     */
    public List<String> search(String prefix) {
        TrieNode currNode = root;
        for (char c : prefix.toCharArray()) {
            TrieNode child = currNode.traverse(c);
            if (child == null) {
                return Collections.emptyList();
            } else {
                currNode = child;
            }
        }
        List<String> matches = new ArrayList<String>();
        preorderTraverse(currNode, matches);
        return matches;
    }

    /**
     * Does preorder traversal of Trie and add retrieved keys in the specified results list.
     * @param currNode
     * @param results
     */
    private void preorderTraverse(TrieNode currNode, List<String> results) {
        if (currNode == null) return;
        if (currNode.getKey() != null) {
            results.add(currNode.getKey());
        }
        Iterator<TrieNode> children = currNode.getChildren();
        if (children != null) {
            while (children.hasNext()) {
                preorderTraverse(children.next(), results);
            }
        }
    }


    public static void main(String[] args) {
        Trie t = new Trie();
        t.insert("vino");
        t.insert("vinod");
        t.insert("vin");
        t.insert("jyo");
        t.insert("jyotsna");
        t.insert("jyot");
        t.insert("jyots");
        t.insert("jyotsn");
        t.insert("joe");

        System.out.println(t.search("vino"));
        System.out.println(t.search("j"));
        System.out.println(t.search("jy"));
        System.out.println(t.search("joe"));

        System.out.println(t.search("bhalblah"));
    }
}



TrieNode class
import java.util.*;

/**
 * A Trie Node
 */
class TrieNode {


    /**
     * The key stored in this node if any.
     */
    private String key;

    /**
     * the outgoing edges of this node, implemented as a sorted map of character to the child node.
     */
    private SortedMap<Character, TrieNode> edges;


    TrieNode addEdge(char c) {
        if (edges == null) {
            edges = new TreeMap<Character, TrieNode>();
        }
        TrieNode childNode = new TrieNode();
        edges.put(c, childNode);
        return childNode;
    }

    TrieNode traverse(char c) {
        return (edges == null) ? null : edges.get(c);
    }

    TrieNode deleteEdge(char c) {
        return (edges == null) ? null : edges.remove(c);
    }

    Iterator<TrieNode>  getChildren() {
        return (edges == null) ? null : edges.values().iterator();
    }

    void setKey(String key) {
        this.key = key;

    }

    String getKey() {
        return key;
    }

    public int getChildrenCnt() {
        return edges == null ? 0 : edges.size();
    }
}


Friday 28 October 2011

[Cricket] Selection time for WI tests

After the sweet cold payback to the English, the Indian selectors meet today to pick the test side for the WI tour of India and a lot of interesting selection decisions need to be taken today keeping in mind the upcoming series down-under.

Dhoni needs to rest, by god!!
I would be really shocked if MS Dhoni is not rested for at-least the test matches, he deserves a break after the long cyclic cricket season which began with IPL-1. He plays (captains, keeps, bats, runs like a bull between the wickets and occasionally rolls his arm over) all the formats of the game and if India wants him to be fresh and rearing to go in the long and testing Australian tour he needs to be given a month or two off. A worn out Dhoni could spell another whitewash down-under. The WI series is the best opportunity to rest Dhoni given that WI have had problems for as long as I can remember batting on the slow-low-spinning Indian wickets and Indians being a very tough opposition on home soil.

If Dhoni is rested and Sehwag takes up the leadership in his absence it would perfectly setup Sehwag's return to test cricket and we can expect atleast a couple of very special innings from him which will prepare him well for the tour down under.

Harbhajan OR Ashwin?
Given recent form its a no-brainer but we have seen in the past how experience is valued more than form. Harbhajan has 400 test wickets to back him but Ashwin has form and variety (carrom-ball) which would be more than a handful for the WI bastmen, I cannot recall the last time Harbhajan bowled a doosra which spun.

I think we all were really happy when Harbhajan was rested (dropped?) for the recent England ODI tour and it would be unfair to drop Ashwin now who took his chance with both hands and did what Harbhajan was doing 5-6 years ago. We all felt Harbhajan need to take a long rest (he is another player who was playing all formats of the game for as long as I can remember without getting any breaks and see what fanatics like me are writing about him now), get back to the nets, find his rhythm and play loads of domestic cricket before he is selected back into the Indian team.

The only thing which goes in favor of Harbhajan is the upcoming series against Australia, an opponent who has always bought the best out of him. If we drop him against WI can we even consider him for the tour down-under?


The Yuvraj-Raina-Kohli battle
Given Gambhir, Shewag, Dravid, Tendulkar and VVS take their deserved positions in the batting line-up and India go in with 4 specialist bowlers + 1 wicket keeper then there is just one position left for Yuvraj, Raina, Kohli.

If India is looking ahead they must back an in-form Kohli at No. 5 and VVS should take the critical No. 6 where he bats along with the tail. I feel particularly sad for Raina because of the energy and commitment he displays, further in Indian conditions he is a devastating batsmen but the fact remains that WI with their able fast bowling line-up are going to test Indian batsmen with a lot of short pitch stuff and one feels Kohli is better equipped to handle those. Also most people view Kohli as one for the future and if that has to work out he has to be given chances when he is in the form of his life and in home conditions so that he grows in confidence in the longer format of the game we well.

Yuvraj and test cricket have never taken well to each other. I felt the England tour was a crucial series for Yuvraj and once again he was found wanting against short pitch bowling. This time around he took one blow too many on his body and his test fortunes.

Who's gonna take the 20 wickets?
Bowling is the key to wining test matches, those 20 wickets which need to be taken. India has traditionally played 3 seamers and 1 spinner but given WI's weakness for spin we might see a 2 + 2 combination this time around. 

If Ashwin is picked as the off spinner it would make sense to compliment him with a leg spinner to so it comes down to Misra/Ojha. I think Mishra edges out Ojha as he possesses the googly along with sharp leg spinner. Also Mishra is a better batsmen than Ojha.

Ishant/Praveen/Sreeshant contest for the 2 seamer spots. Praveen has a big heart but the lack of pace could backfire in Indian conditions where there is little swing and the ball gets old pretty quickly. India should consider atleast one of the two new seamer finds in Umesh Yadav and Varun Aaron specially given Ishant Sharma has postponed his ankle surgery and might take to bed any moment during the 3 tests and we would want someone with pace to replace Ishant if need be.

If I were to pick the 14 it would be

1.   Sehwag (C)
2.   Gambhir (VC)
3.   Dravid
4.   Sachin
5.   Kohli
6.   VVS
7.   Saha (WK)
8.   Ashwin
9.   Mishra
10. Ishant
11. Sreesanth

Reservers : Varun Aaron, Ojha, Ajinkya Rahane.