Data Scientist @Salesforce.com
Background Illustrations provided by: http://edison.rutgers.edu/

Programming Exercise - I Reverse content in a file.

Hello Folks, I want to post one Interview question I was asked by a big company recently. Here is the question. 

Q) Create a file into which huge data is written. Assume data is already there and need not write now. This file is put in the search engine. For now no negative cases like different language or file is empty etc. File is contended with huge data and assume it is very big. Our job is to read the string in file and print in reverse order based on white spaces. Ignore any dots or special characters. 

 For ex : “My name is Ravi and I am 24 years old” should output “old years 24 am I and Ravi is name My old ”

I proposed 3 solutions. 

1. Since it is a large file, I split this into chunks using file splitter lets say 20 10MB files. I create 10 threads and do my operations in parallel and print them. 

2. A straightforward approach which I copied below. Read the file, take in a string and reverse the string and print it. 

3. Use file.seek(offset) which comes from RandomAccess library. Advantage here is we could avoid steps like reading the data in a string and reversing. We directly access it in file and print/write in console or another file respectively. 

To make it simple I tried with 5MB of data, I follows steps 2 and 3 to solve. With Method 2 I could complete it in 14178ms which is invariably bad. Method 3 fetched the same result in 6375ms which I think is still bad but better than former solution. 

package com.myCode.topCoder;

import java.io.*;

import java.util.Scanner;  

public class RevLargeString {

public static  void main(String args[])

{

File f = new File("BigString"); // We can read file instead directly in scanner

//StringBuffer sb = new StringBuffer();  

long startTime = System.currentTimeMillis();

try {

  String rwords =””;

  String printReverse =””;

  Scanner scanner = new Scanner(f);

  while(scanner.hasNextLine())

  {

  rwords = scanner.nextLine();

  System.out.println(rwords);

  }

  String[] reverseWords = rwords.split(" ");

  for(int i= reverseWords.length - 1; i>=0; i—) 

  {

  printReverse += reverseWords[i]+" ";  

  }

  System.out.println(printReverse);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

long endTime = System.currentTimeMillis();

        long totalTime = endTime - startTime;

        System.out.println(totalTime);

}

}



Method 2 Output : 

My name is Ravi and I am 24 years old My name is….

old years 24 am I and Ravi is name My old …….

14178

Method 3 Output : 

My name is Ravi and I am 24 years old My name is….

old years 24 am I and Ravi is name My old …….

6375

I haven’t created classes for 1 and 3. But I can give some idea of how could we do it in method 3. 

Method 3 : 

// Create a randomaccess file instance like          RandomAccessFile r = new RandomAccessFile(“BigString”, “rw”);

// Now start reading backwards in file.                Loop(f.length() to 0)

// Get file pointer. r.getfilepointer();

// Use f.seek(offset) — Here for offset we would give a position where we see a space while looping back. 

// Print from charAt(i) to before occurrence of space. 

Please give suggestions or any other  way we can do it in better time.