Feb 062016
 

So a while back I started doing practice problems in Topcoder for practice. Yesterday at work I noticed that there was one this weekend so I thought why not? The competitive Topcoder match is 1 hour and 15 minutes long, that doesn’t take much out of the day 🙂

Everything started okay, I logged in about 10 minutes before the coding match started. I’m lucky I woke up on time, I didn’t set an alarm..it’s the weekend! I registered and started waiting with everyone else. However when it wouldn’t let me open a match due to timeouts, I refreshed the page. After which I couldn’t login in to Topcoder.com for somewhere between 10-15 minutes after the coding phase had begun. This was frustrating, but I marched forward!

Problem 1:

Given an ordered collection  of coins represented by a string whose state is either ‘H’ or ‘T’, a coin is interesting if one it’s neighbour is a different state. e.g. “HHH” has 0 interesting coins, “HTH” has 3 interesting coins.

Method signature:

My solution felt really clumsy, it takes up a lot of code to check edge cases but maybe it’s necessary. The main strategy I took was to loop through string from 2nd element to second last element and compare neighbours.

 

Problem 2:

Two robots start in positions given by (x1, x1) and (x2,y2) respectively. You are given a string that represents movements that each robot will take e.g., “LRU” means that each robot will move left and then right and then down. Return “Explosion” if there is the possibility that the two robots will cross paths, “Safe” otherwise.

Method signature:

My strategy was to keep a list of all positions of each robot and then check to see if Robot1 ever has a position that Robot2 also had. This passed all but one of the unit tests… I might spend some time tonight debugging it.

EDIT: Oh my… I misread the problem statement…What I said above was my abbreviation of the statement, but it turns out that the robot might skip subsets of instructions.. so unfortunately the code below solves the wrong problem..

This could do with some obvious refactoring, but time constraints really push you to write code fast. Knowing that I misread the problem makes me feel better, since I was bewildered as to why one of the unit tests was failing. If I were to redo the problem I wouldn’t iterate through every position but rather since every sequence defines a rectangle of possible positions, it is enough to see if the intersection of the rectangles that each robot generates has a non zero intersection.

Problem 3:

Given a sequence of n integers, we can compute the bitwise XOR of each pair of integers. This will give us a list of n^2 integers. Your goal in this task is to reverse this process: you will be given the list of n^2 integers and you have to count all sequences of length n that would produce it.

Method signature:

where each integer in the sequence is less than m.

Unfortunately there was only 1 minute left in the competition when I opened this problem. But I’ll give a shot at the problem solving portion. Here is an example of the integers 5 and 3 whose bitwise XOR produce 6

0101
0011 XOR
—–
0110

but this could also be produce by:

1101
1011 XOR
—–
0110

1100
1010 XOR
—–
0110

0100
0010 XOR
—–
0110

So I think the strategy is to track the 0’s in the bitwise XOR. The integer m will give you the maximum number of bits, so if m was 8, only 4 bits would be used, 16, 5 bits would be used etc. So for every pair, there are 2^(max bits – (number of zeros in result of xor)) combinations. Store this result for every pair and multiply them together..or something along those lines 🙂 Following this logic somewhere would likely be fruitful.

Conclusion:

I was nervous trying this, but now I feel addicted and look forward to the next event!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)