Advertisement

Sri Lanka's First and Only Platform for Luxury Houses and Apartment for Sale, Rent

Saturday, September 29, 2012

Google Code Jam 2012 Practice - Reverse Words

In order to improve my algorithm skills I wanted to try out some practice challenges offered by Google Code Jam 2012. I wrote the following code to solve Reverse Words challenge. I verified the output by submitting them to Google Code Jam page and there are correct.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 
 * @author Shazin Sadakath
 *
 */
public class CodeJamSolution2 {
    private File inFile;
    private File outFile;
    
    public static void main(String[] args) {
        if(args.length < 2) {
            System.out.println("Usage : java CodeJamSolution2 <In File> <Out File>");
            return;
        }
        CodeJamSolution2 cjs2 = new CodeJamSolution2(args[0], args[1]);
        cjs2.start();
    }
    
    public CodeJamSolution2(String inFile, String outFile) {
        this.inFile = new File(inFile);
        this.outFile = new File(outFile);
    }
    
    public void start() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(inFile));
            bw = new BufferedWriter(new FileWriter(outFile));
            int count = Integer.parseInt(br.readLine());
            String[] words = null;
            for(int i=0;i<count;i++) {
                words = br.readLine().split(" ");
                reverse(words);
                bw.write(String.format("Case #%d: %s\n", i+1, output(words)));
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * Method to reverse a word array
     * works at O(N / 2) 
     * 
     * @param words
     */
    public void reverse(String[] words) {
        int i=0;
        int j=words.length - 1;
        String temp = null;
        while(i < j) {
            temp = words[i];
            words[i] = words[j];
            words[j] = temp;
            i++;
            j--;
        }
    }
    
    /**
     * method to append all the elements in the array together
     * 
     * @param words 
     * @return appended words sentence
     */
    private String output(String[] words) {
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<words.length;i++) {
            sb.append(words[i]);
            if(i != words.length - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}

Wednesday, September 26, 2012

Google Code Jam 2012 Practice - Store Credit

In order to improve my algorithm skills I wanted to try out some practice challeges offered by Google Code Jam 2012. I wrote the following code to solve Store Credit challenge. I verified the output by submitting them to Google Code Jam page and there are correct.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Store Credit solution 
 * 
 * @author Shazin Sadakath
 *
 */
public class CodeJamSolution1 {
    private File inFile;
    private File outFile;
    
    public static void main(String[] args) {
        if(args.length < 2) {
            System.out.println("Usage : java CodeJamSolution1 <In File> <Out File>");
            return;
        }
        CodeJamSolution1 cjs1 = new CodeJamSolution1(args[0], args[1]);
        cjs1.start();
    }
    
    public CodeJamSolution1(String inFile, String outFile) {
        this.inFile = new File(inFile);
        this.outFile = new File(outFile);
    }
    
    public void start() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(inFile));
            bw = new BufferedWriter(new FileWriter(outFile));
            int totalRecords = Integer.parseInt(br.readLine());
            int credit = 0;
            int items = 0;
            Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();            
            for(int i=0;i<totalRecords;i++) {
                credit = Integer.parseInt(br.readLine());
                items = Integer.parseInt(br.readLine());
                int[] values = new int[items];
                String line = br.readLine();
                int j=0;
                List<Integer> indexes = null;                
                for(String value:line.split(" ")) {
                    values[j] = Integer.parseInt(value);                    
                    indexes = map.get(value);
                    if(indexes == null) {
                        indexes = new ArrayList<Integer>();
                    }
                    indexes.add(j);
                    map.put(value, indexes);
                    j++;
                }                
                insertionSort(values);
                int[] result = findSum(values, credit);    
                int[] finalValues = null;
                if(result[0] == result[1]) {
                    indexes = map.get(String.valueOf(result[0]));
                    finalValues = new int[] { indexes.get(0) + 1, indexes.get(1) + 1};
                } else {
                    finalValues = new int[] { map.get(String.valueOf(result[0])).get(0) + 1, map.get(String.valueOf(result[1])).get(0) + 1};
                    insertionSort(finalValues);
                }                
                bw.write(String.format("Case #%d: %d %d\n", i+1, finalValues[0], finalValues[1]));
                map.clear();                
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
         
    }
    
    /**
     * Method to find sum
     * uses O(N) in worse case given a sorted array
     * 
     * @param values - int Array
     * @param sum - Sum
     * @return int Array with values which add to sum 
     */
    public int[] findSum(int[] values, int sum) {
        int[] result = null;        
        int i = 0;
        int j = values.length - 1;
        while(i <= j) {            
            if((values[i] + values[j]) == sum) {
                result = new int[] { values[i], values[j] };
                break;
            } else if ((values[i] + values[j]) < sum) {
                i += 1;
            } else if ((values[i] + values[j]) > sum) {
                j -= 1;
            }
        }        
        return result;
    }
    
    /**
     * insertion sort algorithm 
     * uses O(N ^ 2) is worse case
     * 
     * @param values - int Array to be sorted
     */
    public void insertionSort(int[] values) {
        int iHole = 0;        
        int item = 0;
        for(int i=1;i<values.length;i++) {
            item = values[i];
            iHole = i;
            while(iHole > 0 && values[iHole - 1] > item) {
                values[iHole] = values[iHole - 1];
                iHole -= 1;
            }
            values[iHole] = item;
        }        
    }    
}

Monday, September 24, 2012

Vulnerability in Opencart based shopping carts

I found a vulnerability which exposes the sensitive information stored in error log file of opencart web application to public. An unprotected web site will cause the log file to be crawled by legitimate bots and will be indexed in search engine result pages. For example if you want to what are the web sites which uses opencart web application as their main shopping cart application just use the following query in google and search.

PHP Warning:  unlink(system/cache/cache.currency [<a href='function.unlink'>function.unlink</a>]: No such file or directory in public_html/system/library/cache.php

This is common warning message logged by almost all of the opencart web applications in their log file. Along with this log file you can find usernames, directory structures etc which are sensitive. An unprotected system and system/log directory in opencart causes this vulnerability. This can be fixed by performing the following step as mentioned in sitefixit.

Securing The /system/ Folder

Certain files are wide-open by default. If you have installed OpenCart in your root directory, just go to http://www.yourdomain.com/system/logs/error.log and you should be able to download your error log, even if you’re a public user. You should protect these files, so create a .htaccess with the following code:


     <Files *.*>
    Order Deny,Allow
    Deny from all
    </Files> 

Then put that .htaccess file in the following 2 directories:
  1. /system/
  2. /system/logs/

Wednesday, September 12, 2012

How I didn't get the Amazon.com Job

Well I was informed by Kristin yesterday (12/09/2012) that there are pursuing with other candidates but I might be considered for future opportunities. Well I am not sad because I took out of good experience through these interviews on what to expect, where should I improve myself on.

One of the main problems I think that got me out of the interview process is that, I didn't ask enough questions from the interviewer. I was trying to find the solution to the problem as quickly as possible. As I think the two algorithms I wrote are in first and last phone screens were not what the interviewers were expecting. Amazon is a company working with millions of records and transactions per day so they might expect their algorithms to be much more efficient than O(n^2). The interviewer never told me this but I guess it is my fault I should have asked.

But I am pretty sure that I did really well during the second phone screen and wrote a very effient solution for the take home question given to me by Pascal. Pascal himself replied to me commending about the cleaness of the code.I am happy and proud about that.

But there are a lot of skills that I should master and this was a good wake up call. Hopefully I work hard on my weaknesses and become good in the future. Anyway as always thank god for giving me the opportunity.


Thursday, August 30, 2012

Amazon.com Interview!!!! Yeah you heard it, Amazon.com! - 3

Thanks to almighty god, I was contacted by Kristin on Friday (24/08/2012) and invited for the 3rd interview which was held on today (30/08/2012).

A Software Development Engineer named Armin contacted me and as usual he asked some general questions followed by technical questions.One of the main questions was;

Given integer array and number k find the two numbers in the array which will add up and become equal to k

The solution I wrote was as below.

int [] inA = new int[6]{1,2,3,4,5,6};
int k = 2

 int[] find ( int[] inArray, int k ) {
     int n1, n2;
     outer:
     for(int i=0;i<inArray.length;i++) {
         for(int j=0;j<inArray.length;j++) {
             if((inArray[i] + inArray[j]) == k && i != j) {
                 n1 = inArray[i];
                 n2 = inArray[j];
                 break outer;
             }
         }
     }
     
     if(n1 == 0 && n2 == 0) {
         return null;
     }
     
     return new int[] { n1, n2 };
 }
The complexity is O(n^2) and Armin was ok with it. Then he asked me whether I have any questions and I asked about my prospective line of work there. He said it will be mostly Payment Transaction processing related development. Anyhow I am thankful to god for giving me this entire experience.

Wednesday, August 22, 2012

Amazon.com Interview!!!! Yeah you heard it, Amazon.com! - 2

Thanks to all mighty god. I got through the first phone interview and was emailed on 17/08/2012 by Kristin to schedule a second phone interview. Usually Amazon has 3 phone interviews prior to an onsite interview. I fixed the next interview to be on Yesterday (21/08/2012) at 9.00 a.m. PT which is 9.30 p.m. local time.

A Software Development Engineer named Pascal Schoenhardt phoned me and he asked some General Questions first. Followed by a technical question which was;

How to traverse a binary tree level by level.

my answer was in Pseudo Code

Function traverse(Node n)
       list.add(n)
       
       while(list not empty) 
           Node n = list.get()
           Print n.value
           
           if(n.leftChild not null)
               list.add(n.leftChild)
           if(n.rightChild not null)
               list.add(n.rightChild)

He seemed ok with the answer. Then he asked me whether I had any questions and I asked about the work environment at Amazon. He explained alot and it seems like a fun place to work in. Then he gave me a Take Home question and asked me to reply to his Email Address the Solution. The problem was


Given:

A log from a website, where each line is a 2-or-3 element CSV sequence containing:
SessionID,Page URL,[Optional Error Code]
The error code field will be populated if an error occurred while the page was loading.
    
Problem:

Parse the log, and produce a list of three page sequences in which lead to an error, 
sorted by occurrance (highest first). That is, every time an error occurs, if the 
sessions in which it occurred has at least two previous successful page loads, we 
have a new three page sequence leading to an error. We are only interested in three 
page sequences. The customer may continue to browse after encountering an error.

Assumptions:

- You have a function getNextLogLine() which returns a CSV string, or null if the end of the log is reached.
- You may assume that you have all of the standard data structures available to you in library form.

Example:

Log File
    123,/products/a.html
    456,/products/a.html
    789,/products/d.html
    789,/products/a.html
    123,/products/b.html
    789,/products/c.html
    456,/products/b.html
    789,/products/d.html,Err2
    123,/products/c.html,Err1
    456,/products/c.html,Err1

Sample Output:            (3 page sequence)                (err)  (count)
    {/products/a.html, /products/b.html, /products/c.html}, Err1, 2
    {/products/a.html, /products/c.html, /products/d.html}, Err2, 1


I was staying awake and wrote the solution and mailed him back with the answer. The interview was a great experience and as always I am thankful to god for giving me this opportunity.


Thursday, August 16, 2012

Amazon.com Interview!!!! Yeah you heard it, Amazon.com!

What a night? All praise to god for giving me this opportunity. I was contacted by a Amazon.com recruiting coordinator named Kristin last week explaining that their Recruiting Manager has apparently seen my CV on monster.com and really interested to have a phone interview with me. I was over the moon! but to my horror I didn't see a reply up until last Tuesday but then Kristin finally contacted me and fixed a date for a phone interview. I decided the date to be Thursday 9.00 a.m. PT.

A Software Development Engineer named Yanlin phoned me and we had the interview for approximately one hour. The guy is really nice and asked me general as well as technical questions. One of the major coding questions was the following

Given two arrays of numbers of same size.  For elements in one array, assume that you can always find a matching element in the other array, except for one element.  So what you got is two elements that differ from each other and all rest should match in the two arrays.  Write a function to print out these two numbers.

My solution which I wrote within 15 minutes is as follows. Not the ideal or most efficient one but it works ;).


public static void findNonMatching(int[] a, int[] b) {
    if(a.length != b.length) {
        return;  // Yan - given the question requirement, validation check.
    }
    
     // Yan - optimize when
     for(int i=0;i<a.length;i++) {
         boolean foundEqualA = false;
         boolean foundEqualB = false;
         inner1:
         for(int j=0;j<b.length;j++) {
             if(a[i] == b[j]) {
                 foundEqualA = true;
                 break inner1;
             }
         }
         inner2:
         for(int j=0;j<b.length;j++) {
             if(b[i] == a[j]) {
                 foundEqualB = true;
                 break inner2;
             }
         }
         if(!foundEqualA) {
             System.out.println("In A " +a[i]);             
         }
         if(!foundEqualB) {
             System.out.println("In B " +b[i]);
         }
     }

}

The complexity is O(n^2). You can see comments by Yanlin himself.Anyway I am quite happy and thankful to god for giving me this opportunity. As the famous saying says "Experience is what you get, When you didn't get what you want". Even if I don't get this the experience will be there for a life time.