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
Joined
9 Mar 2010
Posts
2,841
I've done one MANY moons ago.

It was a simple proprietary system they used that was basically a simplified codingbat.com that allowed them to see what I was typing.

At the same time we were chatting via webex.

It's no different than doing one on site apart from the fact you'll easily get away with referencing documentation when in the heat of the moment you forget how to format a for loop :)
 
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
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Unlucky about the job. I always find it a bit odd when they give you a mark for things like this.
They've obviously got an idea about how they would do it and you're being marked on how similar your solution is to this version.

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?

Also this has reminded me how much I love stuff like LINQ, processing streams of data feels so clunky doing it in a non-functional way.
 
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
Joined
18 Oct 2002
Posts
3,926
Location
SW London
It's just that you're iterating the collection twice and doing extra work.
Nothing major, but if I was marking something like this it's something I'd pick up on and you could well have lost marks for that.

You could just iterate over the collection once, extract the keys inline and do the check against the deleted items set there.
I'm on mobile right now, so hard to give an example but hopefully you understand what I mean!
 
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
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Yep, that's the sort of thing I was thinking of.
If I was critiquing the code for interview purposes I'd pick that up. It will be interesting to see what sorts of things they mention if you ever do get any feedback on it.
 
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