Friday, January 16, 2015

Java Code to find all paths in a grid from start to end.

/*

 1  2 3  4
 5  6   7  8
 9 10 11 12
 13 14  15  16

TC1: invalid grid.
TC2: smaller than len-1 x len-1.
TC3: Functional
           all possible routes.

Returns: number of possible routes from (0,0) to (len-1,len-1) if success,
         -1 on failure.
*/

public int numberOfPossibleRoutes(int[][] grid, int currentRow, int currentColumn, int length)
{
    if( grid  == null )
    {
        return -1;
    }
   
    if( currentRow == length-1 && currentColumn == length-1)
    {
        return 1;
    }
   
    int countToReturn=0;
    // Count the number downwards.
    if( currentRow < length - 1)
    {
        countToReturn += numberOfPossibleRoutes(grid, currentRow+1, currentColumn, length);  
    }
    else if( currentRow == length - 1)
    {
        // Only right count.
        return numberOfPossibleRoutes(grid, currentRow, currentColumn+1, length);
    }
    else
    {
        throw new ArrayIndexOutOfBoundsException();
    }
   
    // Count right possibilities
    if( currentColumn < length-1)
    {
        countToReturn += numberOfPossibleRoutes(grid, currentRow, currentColumn+1, length);   
    }
    else if( currentColumn == length-1)
    {
        return numberOfPossibleRoutes(grid, currentRow+1, currentColumn, length);
    }
    else
    {
        throw new ArrayIndexOutOfBoundsException();
    }
    
    return countToReturn;
}

Tuesday, January 13, 2015

Java Code to Convert Binary Tree to Circular Doubly Linked List

/*
This function processes Binary Tree as show below
                               1
                 2                           3
      4                5
to circular doubly linked list as shown


4 <-> 2 <-> 5 <-> 1 <-> 3

Also links to 4 and 3 are updated.
*/

public void convertBTToCDLL(Node current, Node toReturnHead, Node toReturnTail)
{
    // Exit conditions and Null handling here.
    if( current == null )
    {
        return;
    }
   
    // convert left sub tree to CDLL
    Node leftSubTreeHead=null;
    Node leftSubTreeTail=null;
    if( current->left )
    {
        convertBTToCDLL(current->left, leftSubTreehead, leftSubTreeTail);
    }
   
    // convert right sub tree to CDLL
    Node rightSubTreeHead=null;
    Node rightSubTreeTail=null;
    if(current->right)
    {
        convertBTToCDLL(current->right, rightSubTreeHead, rightSubTreeTail);
    }
  
    // Convert the current node to CDLL 
    if(! leftSubTreeTail)
    {
        leftSubTreeTail = leftSubTreeHead = current;
  
    }
       
    if(! rightSubTreeHead)
    {
        rightSubTreeHead = rightSubTreeTail = current;
    }


    leftSubTreeTail->right = current;
    current->left = leftSubTreeTail;

    rightSubTreeHead->left = current;
    current->right = rightSubTreeHead;

    // Make this circular
    toReturnHead = leftSubTreeHead;
    toReturnTail = rightSubTreeTail;
}

Friday, January 2, 2015

Best way to engineer software products!

  1. Gather Requirements thoroughly.
  2. Make High Level Architecture.
  3. Write Sequence diagrams.
  4. Make Low Level design.
  5. Make Data Model.
  6. Write Use Case based Test Cases.
  7. Identify Objects, Methods and APIs.
  8. Identify Design patterns, files organization, library structure.
  9. And Coding should be brain dead job (mostly). 
Gather Requirements thoroughly:
This is where it starts. The right questions to ask at this point is :
  1. What problem does this product solve?
  2. How will it impact customers?
  3. In what way will the end-product look like? 
  4. In what way shall the requirements change?
  5. How much load should the product serve?
  6. To what extent, should it scale reasonably?
Make High Level Architecture:
At this step, identifying large pieces (or blocks) that contain reasonable ambiguity. See how those blocks are connected to each other. Right questions to ask at this point is:
  1. What could be the role of each block in serving customers?
  2. How many different types of customers exist in the system?
  3. What choice of database would be most suitable?
  4. How many varieties of data stores (or tables ) would be needed?
Write Sequence diagrams:
These define the interactions between each block in the high level architecture.

Make Low level design:
This point is to break a lot of ambiguity by breaking each block into various smaller components. Making flow charts of how each smaller components would interact with each other should be done here.

Make Data Model: 
At this point, define how the data in the system flows from the customer to the database and from the database to the customer. Typically identification of what exact fields are required in the database would be done here.

Write Use Case based Test Cases:
Use cases are typically defined as one task a customer achieves in the product. Writing these as exhaustively as possible helps a lot while going to the next level. These test cases would come really handy in absorbing ambiguity while coding.


Rest are self-explanatory.

At each of these steps, one needs to keep an eye on how fast things could change and each step should reasonably adapt to changes in the environment.