Contest: HackerRank Women’s Cup
This weekend I competed in the HackerRank Women’s Cup contest. And yes… I checked with HackerRank to make sure that I was *eligible* to compete even though I’m not a woman. 🙂 There was a catch however. Though I could compete, I was *not eligible* to win any prizes. Those were reserved only for women.
Anyway, I placed 45 out of 1137 participants, which was a pretty good placement. I scored full points on the first 6 problems, but only partial points on the last one (since 15/33 tests passed on that one, but the rest timed out). I tried many different things to speed up the last problem (e.g. using interval trees), but the optimal solution involved using segment trees again.
Here is a summary of the problems, and my solutions:
# | Problem | My Solution | Algorithmic Technique | Time Complexity | Space Complexity | Level |
1 | Possible Maximum | GitHub | Basics | O(n^2) | O(1) | Easy |
2 | Project Earth | GitHub | Math, AdHoc | O(1) | O(1) | Easy |
3 | Magical Pearls | GitHub | Heaps | O(n log n) | O(n) | Moderate |
4 | Cabalistic Hills | GitHub | Eulerian Cycle, Graphs | O(|E| + |V|) | O(|E| + |V|) | Moderate |
5 | Magical String | GitHub | Pre-compute
AdHoc |
O(n) precomp
O(1) query |
O(n) | Moderate |
6 | Coding Camp | GitHub | Sorting, AdHoc | O(m^2 log m) | O(m+n) | Difficult |
7 | Counting Pairs | GitHub | Brute Force | Query 1 O(1)
Query 3 O(1) Query 2 O(min(n, (s2-s1))) |
O(n) | Difficult |
Here’s a fun problem from the contest:
Problem: Cabalistic Hills
Problem Statement
Antony and Cleopatra are true soul mates and, like true soul mates, they fight. Instead of giving her an elixir like last time, Antony wants to bestow her with the magical lamp available at the Cabalistic Hills.
The Cabalistic Hills were created by Lucifer millions of years ago. They have N hills numbered 1 to N. The magical lamp is situated at the peak of hill N. Some of these hills are connected by one way rope bridges(directed), and these rope bridges are the only way to reach hill N and return back to hill 1. Each rope bridge disappears after being used once.
Lucifer has created traps for the climbers. Some rope bridges do not lead to hill N, and some lead to hill N but without a way to return back to hill 1. Antony MUST use all the rope bridges in his journey from hill 1 to N and back to 1 or he is imprisoned by Lucifer.If multiple ropes are connecting two hills then it should be counted multiple times and all of the ropes must be used once. You must advise Antony whether the hills are safe to climb or they are devious trap! Given N hills and M pairs of connected hills, determine whether it’s a trap.
Input Format
The first line contains T: the number of test cases. Each test case contains N and M separated by space. Next M lines contains v1, v2 separated by a space where v1 and v2 represent the hills connected by a rope bridge.
Constraints
1≤T≤15
1≤N≤10^5
0≤M≤2×10^5
Output Format
Output T lines. For each test case, either print “Go on get the Magical Lamp” or, if it’s a trap, “Danger!! Lucifer will trap you” without quotes.
Sample Input
2
4 4
1 2
2 3
3 4
4 1
4 3
1 2
2 3
3 1
Sample Output
Go on get the Magical Lamp
Danger!! Lucifer will trap you
Solution
As kids, we have all at some point been posed the Boyd Puzzle. The riddle asks us to draw a certain figure, starting from one point, and ending at the same point with these rules:
- You cannot lift your pencil off the paper.
- You cannot cover the same line twice.
- You must draw all the lines in the figure.
This is an unsolvable riddle, for the particular figure posed, and there is actually a mathematical theorem that proves the same. In fact the famous mathematician Euler proved it rigorously with his famous problem “Seven Bridges of Konigsberg”. This was a major historical development, and laid the foundations of graph theory, which is so ubiquitous in computer science!
So what does this have to do with our current problem? Our problem is a generalized version of this Boyd’s Puzzle, where we need to give a yes/no answer to “Is it possible to construct a Eulerian cycle for the provided input?”
i.e. Let’s construct a graph with the input. Now if you read the problem statement carefully you will notice the following.
- We must start at 1, visit N, and come back to 1 (i.e. trace a cycle).
- We must also visit *every edge*, only in the direction stated.
- We can visit every edge exactly once (the bridges disappear after travel).
- We *must* use all bridges.
We see that this problem is a sneaky way of asking us to detect a Eulerian Cycle in the graph! Once you realize this, the problem becomes extremely simple. Indeed, there exists a polynomial time algorithm to do this. The algorithm is as follows.
A directed graph has a Eulerian Cycle if and only if:
- All vertices with in/out edges belong to the same single strongly connected component.
- For each vertex in-degree(v) == out-degree(v)
We can do #1 above by running Kosaraju’s SCC algorithm like one of our previous contests. We can keep some bookkeeping information along the way to figure out #2 quite easily as well.
Note: The diagram below is credited to mathworld since I was too lazy to draw my own.
It is interesting that detecting/constructing a Eulerian Cycle (using all edges exactly once) is a polynomial time algorithm, but constructing a Hamiltonian Cycle (using all vertices exactly once) is an NP-Complete problem!