Thursday, December 25, 2014

Java program to find the common interval between the meeting intervals of two persons


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Meeting {
 private static List<Double> PS1; // Person 1's Meeting Start times.
 private static List<Double> PE1; // Person 1's Meeting End times.
 private static List<Double> PS2; // Person 2's Meeting Start times.
 private static List<Double> PE2; // Person 2's Meeting End times.
 private static Double maxTime;
 public static void main(String[] args) {
  //TC 1
//  PS1 = Arrays.asList(0D,8D);
//  PE1 = Arrays.asList(1D,23D);
//  PS2 = Arrays.asList(0D,3D,5D,9D);
//  PE2 = Arrays.asList(2D,4D,6D,23D);
  
  //TC 2
  PS1 = Arrays.asList(1D);
  PE1 = Arrays.asList(2D);
  PS2 = Arrays.asList(2D);
  PE2 = Arrays.asList(3D);
  
  //TC3
  PS1 = Arrays.asList(1D,10D,19D,21D,27D);
  PE1 = Arrays.asList(5D,14D,20D,23D,30D);
  PS2 = Arrays.asList(3D,12D,18D,23D);
  PE2 = Arrays.asList(5D,15D,21D,24D);
  
  maxTime = PE1.get(PE1.size() - 1) > PE2.get(PE2.size() - 1) ? PE1.get(PE1.size() - 1) : PE2.get(PE2.size() - 1); 
  
  FreeTimeSlot P1 = findFreeTimeSlot(PS1,PE1);
  System.out.println("Free time slots for person 1 :");
  printTimeSlot(P1);
  
  FreeTimeSlot P2 = findFreeTimeSlot(PS2,PE2);
  System.out.println("Free time slots for person 2 :");
  printTimeSlot(P2);

  FreeTimeSlot commonFreeTimeSlot = findCommonFreeTimeSlot(P1, P2);
  System.out.println("Common free time slots for person 1 and person 2 :");
  printTimeSlot(commonFreeTimeSlot);
 }
 
 public static void printTimeSlot(FreeTimeSlot timeSlot) {
  int i=0;
  for(Double time : timeSlot.S) {
   System.out.println(time + " " + timeSlot.E.get(i));
   i++;
  }
 }
 
 public static FreeTimeSlot findCommonFreeTimeSlot(FreeTimeSlot P1, FreeTimeSlot P2) {
  FreeTimeSlot commonFreeTimeSlot = new FreeTimeSlot();
  int a = 0; int b = 0;
  //a is the index for Person 1
  //b is the index for Person 2
  while (a < P1.S.size() && b < P2.S.size()) {
   //Person 1's time slot lies completely ahead of Person 2's time slot
   /*
    *        a                    b
    * +-----------------+   +----------------+
    */
   if(P1.S.get(a) < P2.S.get(b) && P1.E.get(a) <= P2.S.get(b)) {
    a++;
   }
   //Person 2's time slot lies completely ahead of Person 1's time slot
   /*
    *        b                    a
    * +-----------------+   +----------------+
    */
   else if(P1.S.get(a) > P2.S.get(b) && P1.S.get(a) >= P2.E.get(b)) {
    b++;
   }
   //Start of Person 1's time slot is before start of Person 2's time slot
   /*
    *        a        b
    * +-------------+---+-------+
    */
   else if(P1.S.get(a) <= P2.S.get(b)) {
    commonFreeTimeSlot.S.add(P2.S.get(b));
    Double time = P1.E.get(a) < P2.E.get(b) ? P1.E.get(a) : P2.E.get(b);
    commonFreeTimeSlot.E.add(time);
    if(P2.E.get(b) < P1.E.get(a)) {
     b++;
    }
    else {
     a++;
    }
   }
   //Start of Person 2's time slot is before start of Person 1's time slot
   /*
    *        b        a
    * +-------------+---+-------+
    */
   else if(P1.S.get(a) > P2.S.get(b)) {
    commonFreeTimeSlot.S.add(P1.S.get(a));
    Double time = P2.E.get(b) < P1.E.get(a) ? P2.E.get(b) :  P1.E.get(a);
    commonFreeTimeSlot.E.add(time);
    if(P1.E.get(a) < P2.E.get(b)) {
     a++;
    }
    else {
     b++;
    }
   }
  }
  return commonFreeTimeSlot;
 }
 
 public static FreeTimeSlot findFreeTimeSlot(List<Double> personStart, List<Double> personEnd) {
  double s=0;
  int a = 0;
  FreeTimeSlot freeTimeSlot = new FreeTimeSlot();
  while(a<personStart.size()) {
   if(s < personStart.get(a)) {
    freeTimeSlot.S.add(s);
    freeTimeSlot.E.add(personStart.get(a));
    s = personEnd.get(a);
    a++;
   }
   else if(s == personStart.get(a)) {
    s = personEnd.get(a);
    a++;
   }
  }
  // Adding the remaining time of the day as a free time slot.
  int length = freeTimeSlot.E.size();
  if(freeTimeSlot.E.get(length - 1) < maxTime && personEnd.get(personEnd.size() - 1) < maxTime) {
   freeTimeSlot.S.add(personEnd.get(personEnd.size() - 1));
   freeTimeSlot.E.add(maxTime);
  }
  return freeTimeSlot;
 }
}

class FreeTimeSlot
{
 List<Double> S = new ArrayList<Double>();
 List<Double> E = new ArrayList<Double>();
}

Sunday, December 21, 2014

Print combinations of strings from List of List of Strings

Print combinations of strings from List of List of Strings.
Example input: [[abc, def], [ghi, jkl, mno], [pqr]]
Example output:
abc ghi pqr 
abc jkl pqr 
abc mno pqr 
def ghi pqr 
def jkl pqr 
def mno pqr 

Java Program

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Combinations {
 public static void main(String[] args) {
  int num = 0;
  ArrayList<List<String>> input = new ArrayList<List<String>>();
  input.add(Arrays.asList("abc","def"));
  input.add(Arrays.asList("ghi","jkl","mno"));
  input.add(Arrays.asList("pqr"));
  ArrayList<String> output = new ArrayList<String>();
  combine(num, input, output);
 }
 
 public static void combine(int num, ArrayList<List<String>> input, ArrayList<String> output) {
  if(num == input.size()) {
   for(String string : output) {
    System.out.print(string + " ");
   }
   System.out.println();
  }
  else {
   List<String> current = input.get(num);
   for(String string : current) {
    output.add(string);
    combine(num + 1, input, output);
    output.remove(num);
   }
  }
 }
}

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;
 }
}