Remote technical test - interview

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I have a remote technical test in Java tomorrow night (from home) for a potential job starting next year. It's two hours long and the recruitment agent was pretty vague as to what it involved.

Has anyone ever done something like this?

I've done technical tests before, but they've been either multiple choice or problem solving stuff on codingbat.com
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I'll be able to tell you soon, starts in 10 minutes. What I do know

- Marked out of 20 (not sure if that means 20 questions)
- Not multiple choice
- I submit something that I have to write and it is then reviewed by someone.
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Wow that was quite hard work, a lot to do in two hours, did it in 2hrs 10mins. And my program has a bug in it which I didn't have time to work out :( (but it does only happen some of the time), I figured best submit something that's almost there sooner than later

Basically first part was we were given a spec which we had to write requirements for how it could be implemented, with enough detail that another developer could implement it.

Second part was a file containing key value pairs 277000 of them, the program had to create another file which contained a random list of 10000 keys from that file. After that I had to create a new file which was a list of key value pairs with the key value pairs from the second file removed.

A lot to do and I did go over, but I hope they review my answers anyway.
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Realised shortly after submitting I had misread the code question, it bothered me so I got out of bed and fixed my mistakes, I've sent them the correction. Hope they view that well. Now I'm off to bed :p
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Thanks, I hope they're not too strict on the timing. It did say please aim to have it complete in two hours.

I also hope they review the code I got out of bed to do and not the version where I misread part of the question.

Dunno I'm hoping they view it as a positive that I got out of bed to fix my mistakes. Hehe, I haven't had much sleep :p
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I've actually spent a little bit of time this morning refactoring a couple of bits of the code I sent them.

Basically they provided two input files, one called items which had things like this

Code:
10822550,7,68913
10822549,1,68913
10822551,2,68913
10822552,8,68913
10822553,4,68913
10822555,3,68913
10822556,6,68913
10822554,5,68913

And another file called ItemsToDelete with contents like this

Code:
10749125
10810448
13779335
10849792
10758263
10727028

The idea was to create a new third file that had everything from the first file, minus the keys that appear in the second.

My solution was this

Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ItemsToDelete {

  private static final String NEW_ITEMS = "C:\\xxx\\NewItems.txt";

  public static void main(String[] args) {
    long startTime = System.nanoTime();
    
    Path items = Paths.get("C:\\xxx\\Items.txt");
    Path itemsToDelete = Paths.get("C:\\xxx\\ItemsToDelete.txt");

    try(PrintWriter writerItemsNew = new PrintWriter(NEW_ITEMS, "UTF-8")  ) {
      String itemsContents = new String(Files.readAllBytes(items));
      Map<String, String> itemsMap = stringToMap(itemsContents);          

      String itemsDeleteContents = new String(Files.readAllBytes(itemsToDelete));
      Set<String> deleteKeys = new HashSet<>(Arrays.asList(itemsDeleteContents.split("\r\n")));      

      Set<String> keySet = itemsMap.keySet();
      for(String key : keySet) {
        if(!deleteKeys.contains(key)) {
          writerItemsNew.println(key +"," +itemsMap.get(key));  
        }               
      }
      
      long endTime = System.nanoTime();
      
      System.out.println("Time to complete: " + (endTime - startTime));

    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }      

  }

  public static Map<String, String> stringToMap(String mapString) {
    Map<String, String> map = new HashMap<>();
    String[] lines = mapString.split("\r\n");
    for(int i=0; i<lines.length; i++) {
      String[] keyValue = lines[i].split(",", 2);
      map.put(keyValue[0], keyValue[1].trim());
    }
    return map;
  }


}

So that's what I sent, here's it tidied up a bit with some comments, perhaps this would have got me a couple more marks? Thoughts?

Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ItemsToDelete {

  private static final String ITEMS           = "C:\\xxx\\Items.txt";
  private static final String ITEMS_TO_DELETE = "C:\\xxx\\ItemsToDelete.txt";
  private static final String NEW_ITEMS       = "C:\\xxx\\NewItems.txt";

  public static void main(String[] args) {
    long startTime = System.nanoTime();

    createNewItems();

    long endTime = System.nanoTime();

    System.out.println("Time to complete: " + (endTime - startTime));
  }

  /**
   * This method creates the new items file, which is the contents
   * of the original items files, minus the keys that appear in
   * the ItemsToDelete file
   */
  private static void createNewItems() {
    Path items = Paths.get(ITEMS);
    Path itemsToDelete = Paths.get(ITEMS_TO_DELETE);

    try(PrintWriter writerItemsNew = new PrintWriter(NEW_ITEMS, "UTF-8")) {
      String itemsContents = new String(Files.readAllBytes(items));
      
      Map<String, String> itemsMap = stringToMap(itemsContents);          

      String itemsDeleteContents = new String(Files.readAllBytes(itemsToDelete));
      Set<String> deleteKeys = new HashSet<>(Arrays.asList(itemsDeleteContents.split("\r\n")));      

      Set<String> keySet = itemsMap.keySet();
      for(String key : keySet) {
        if(!deleteKeys.contains(key)) {
          writerItemsNew.println(key +"," +itemsMap.get(key));  
        }               
      }

    } catch (IOException e1) {
      e1.printStackTrace();
      System.exit(1);
    }    
  }

  /**
   * This method converts the contents of the items file into a Map
   * data structure so that the UDP key and it's value can be easily
   * retrieved when creating the new items file. Maps are extremely 
   * efficient and provide O(1) complexity
   * 
   * @param itemsContents - String contents of the items file
   * @return
   */
  private static Map<String, String> stringToMap(String itemsContents) {
    Map<String, String> map = new HashMap<>();
    String[] lines = itemsContents.split("\r\n");
    for(int i=0; i<lines.length; i++) {
      String[] keyValue = lines[i].split(",", 2);
      map.put(keyValue[0], keyValue[1].trim());
    }
    return map;
  }

}
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Not sure why you need to put the original list into a map though? You're going to have to iterate over all of them anyway so what does a map give you?

It felt easier to pick out the keys I wanted to appear in the new file then

Code:
     Set<String> keySet = itemsMap.keySet();
      for(String key : keySet) {
        if(!deleteKeys.contains(key)) {
          writerItemsNew.println(key +"," +itemsMap.get(key));  
        }               
      }

Unless I'm missing something obvious :confused:
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Ah yes I see what you're saying, I'm good at overcomplicating stuff.

I think you mean something like this

Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ItemsToDelete {

  private static final String ITEMS           = "C:\\xxx\\Items.txt";
  private static final String ITEMS_TO_DELETE = "C:\\xxx\\ItemsToDelete.txt";
  private static final String NEW_ITEMS       = "C:\\xxx\\NewItems.txt";

  public static void main(String[] args) {
    long startTime = System.nanoTime();

    createNewItems();

    long endTime = System.nanoTime();

    System.out.println("Time to complete: " + (endTime - startTime));
  }

  /**
   * This method creates the new items file, which is the contents
   * of the original items files, minus the keys that appear in
   * the ItemsToDelete file
   */
  private static void createNewItems() {
    Path items = Paths.get(ITEMS);
    Path itemsToDelete = Paths.get(ITEMS_TO_DELETE);

    try(PrintWriter writerItemsNew = new PrintWriter(NEW_ITEMS, "UTF-8")) {
      String itemsContents = new String(Files.readAllBytes(items));

      String itemsDeleteContents = new String(Files.readAllBytes(itemsToDelete));
      Set<String> deleteKeys = new HashSet<>(Arrays.asList(itemsDeleteContents.split("\r\n")));
      
      String[] lines = itemsContents.split("\r\n");
      for(int i=0; i<lines.length; i++) {
        String[] keyValue = lines[i].split(",", 2);
        String key = keyValue[0];
        if(!deleteKeys.contains(key)) {
          writerItemsNew.println(key +"," + keyValue[1].trim());          
        }
      }      

    } catch (IOException e1) {
      e1.printStackTrace();
      System.exit(1);
    }    
  }  

}

Saved a whole 0.2 seconds, so 0.4 seconds to read two input files totalling 285776 lines and writing out a new one of 265776 lines, so quite good I suppose
 
Last edited:
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
This is part of the reason I want to move, no proper critique in this company. I tried critiquing my colleagues code this morning because he has something like this in his code.

Code:
try{
//blah
}catch(ArrayIndexOutOfBoundsException e) {
  //This was actually some logging code but you get my point
  System.out.println("Woops!");
}

I was of course ignored.
 
Back
Top Bottom