Wednesday, November 26, 2014

Match the columns problem

Problem:
Find the number of intersections of the lines matching the two columns. The first line contains an integer T denoting the number of test-cases.
First line of each test-case contains an integer N denoting the number of elements in each column. N lines follow each containing a character c separated from a number n by a space character.

Sample Input:
1
8
A 3
B 2
C 8
D 1
E 6
F 7
G 4
H 5

Sample Output:
10 

Explanation:




Solution: To come in following days.

Monday, November 24, 2014

Switching between tabs in same browser window using Selenium WebDriver

Recently I was supposed to automate a test-case which needed me to perform the following steps:

1. Open a browser window
2. Navigate to a URL
3. Perform some operations
4. Open a new tab in the "same" browser window
5. Perform some operations in the newly opened tab
6. Switch back to the previously opened tab
7. Perform some operations in this tab

Most of the resources I have come across while I was dealing with this particular problem talked about opening a new browser window (not a new tab in the same window), using windowHandles to switch between the windows.

The typical catch in this problem is:
When you open a new browser window, you get two window handles. Then they can be used to switch between them.
But when you open a new tab, there is a single window handle. So switching between tabs is not achieved by calling driver.switchTo().window(<windowHandle>)

So I used the following work-around:
1. Open a new tab using Ctrl + t
2. Driver control automatically switches to the newly opened tab
3. Perform the required operations here.
4. Next switch back to the old tab using Ctrl + Tab. You need to keep pressing this unless you reach the desired tab.
5. Once the desired tab is reached, then perform the operations in that tab.

Sample Code:
Get the current window handle and open a new tab using Ctrl + t
        driver.get("http://google.com");
        String windowHandle = driver.getWindowHandle();
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

The size of the output of getWindowHandles() is 1 which means that there is one single window handle
        ArrayList tabs = new ArrayList (driver.getWindowHandles());
        System.out.println(tabs.size());
        driver.switchTo().window(tabs.get(0)); 

The control is now in the new tab
        driver.get("http://bing.com");
        //perform other operations.

Switch to the old tab using Ctrl + Tab
        driver.switchTo().window(mainWindowHandle);
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
        driver.switchTo().defaultContent();

Sunday, November 23, 2014

String Problems asked in Programming Interviews

Problem 1:
Find the longest anagram substring of a given string S2 in another string S1.
S1: ascfg
S2: acs
Output: asc



Problem 2:
Find all the anagram substrings of a given string S2 in another string S1.
S1: ascfgsa
S2: cas
Output: {asc, sa}

Saturday, November 22, 2014

Programming problems which can be solved using recursion


Problem 1: 
There is a terrain on which water flows from West to East. As per laws of Physics, water will flow from higher height to lower height. Find the number of possible paths on which water can flow on the terrain.
The terrain is described by a two-dimensional array A of dimensions M x N.
Each element A[i,j] specifies the height of the point.
Water can only flow from one cell to another cell if one of the following conditions satisfy:
1. A[i][j+1] < A[i][j]
2. A[i-1][j+1] < A[i][j]
3. A[i+1][j+1] < A[i][j]

Input: 
The first line of input will consist of a number N which determines the number of test-cases.
The second line contains two space-separated integers M, N denoting the dimensions of the two-dimensional array.
The next M lines contain N space separated integers each denoting the height of each point A[i,j] of the terrain.

Output:
Output should contain N lines each containing the number of possible paths water can flow from West to East (left-to-right).

Example:










Explanation: 
In the diagram above, there are four paths water can flow from left to right.
1. 10 -> 8 -> 6
2. 10 -> 8 -> 7
3. 12 -> 8 -> 6
4. 12 -> 8 -> 7

Saturday, November 1, 2014

Largest sum of any of the paths till row N-1 of a two-dimensional array

Approach explained pictorially:



Initial matrix


After first iteration


After second iteration
public class Matrix {
 public static void main(String[] args) {
  //Taking N = 3, for 3x3 matrix.
  int[][] A = {{1,2,3},
               {6,4,5},
               {9,8,7}};
  
  //Dynamic programming approach.
  //Logic is to compute the current maximum sum at any given point in the two-dimensional array.
  //Time complexity: O(N^2).
  for(int i = 1;i < A.length;i++) {
   for(int j = 0;j < A.length; j++) {
    if(j == 0) {
     A[i][j] = max((A[i][j] + A[i-1][j]),(A[i][j] + A[i-1][j+1]));
    }
    else if(j==A.length - 1) {
     A[i][j] = max((A[i][j] + A[i-1][j]),(A[i][j] + A[i-1][j-1]));
    }
    else {
     A[i][j] = max((A[i][j] + A[i-1][j-1]),(A[i][j] + A[i-1][j]),(A[i][j] + A[i-1][j+1]));
    }
   }
  }
  
  //The maximum sum is present in the last (N-1)th row. Traverse it to find the maximum sum in O(N) time complexity.
  int maxSum = Integer.MIN_VALUE;
  for(int i=0;i<A.length;i++) {
   if(maxSum < A[A.length - 1][i]) {
    maxSum = A[A.length - 1][i];
   }
  }
  
  System.out.println(maxSum);
 }
 
 //Returns the maximum of the two numbers passed as arguments.
 public static int max(int a, int b) {
  return a >= b ? a:b;
 }
 
 //Returns the maximum of the three numbers passed as arguments.
 public static int max(int a, int b, int c) {
  int x = (a >= b ? a:b);
  return c >= x ? c:x;
 }
}