This is about a programming problem that kept me bugging for few days. In a popular site hosting coding challenges, I faced this problem where I was to handle connections between nodes, and decide whether a specific pair of nodes are connected or not.
The first approach I tried using 2D arrays - a pretty easy one, but it grossly failed when number of nodes crossed some 20000. I laughed at myself, why in the first place I have thought of allocating some 20000 x 20000 bytes in memory! It would neatly bring a heap space error in no time.
I got another trump - hashing. Using HashMap and HashSets (I am a java person, by the way) to represent a graph structure. And a crisp search logic (over BFS or DFS) - that's it! My code was ready in no time.
And it failed in no time!
Recursion in the search logic soon went out of boundary bringing up a Stack Overflow.
I scratched my head over the problem, changing this and that to alter the flow, but I couldn't make it run for more than some 30000 nodes. Next I was thinking of Threading in recursion, but anyhow left it there to think fresh the day after.
Ideas come to me at night. It strikes. The next day I used a 1D array, positions denoting the nodes and values denoting a cluster that node belongs to. In absence of hashing, iterations were a bit more time consuming, still the code scaled well. Searches were fast, and it handled some 600000 nodes in 12.6 minutes.
I couldn't submit the solution, though, because my code was developed on linux simulated on a windows machine, and it was failing either on build or compilation on the server where I was to upload the solution.
I always believe that there is a simpler solution to any problem than the simplest ever found. There has to be. Adding recursion to this view, there is no end of finding a better solution, only you need to have the patience and dedication to find it.
Note 1: I will decide on a better title of this post later on.
Note 2: Does the view work in real life? I am currently thinking of instances.
Sunday, October 23, 2011
Why am I in office every weekend?
I work in an IT farm in India, away from my hometown, and I am connected to my friends and relatives mostly over phone calls. When they call me up on weekends, and get to know that I am in office, the same expression follows - 'hey man, your job sucks ... office in weekends !'
Why? Can't a software developer working in an IT farm love his work? Agree, that is probably not the case with most of the people out there, but still I don't understand why it is tough to believe one is satisfied with his work in this field.
I love reading books in the night. I love to travel, but not with the sun blazing overhead. Work keeps me better than a movie, sometimes, in these dry noons. The calmness and solitude at office, and a soothing tone in my player, makes me think better and work better. And by work I mean the work I love to do, not those assigned to me by my job.
I am writing this post too on a Sunday noon, from my office. Few years down the line, who knows, looking back on it, I might feel the same about myself - 'you were crazy man - making it to office on weekends!'. But not today.
Why? Can't a software developer working in an IT farm love his work? Agree, that is probably not the case with most of the people out there, but still I don't understand why it is tough to believe one is satisfied with his work in this field.
I love reading books in the night. I love to travel, but not with the sun blazing overhead. Work keeps me better than a movie, sometimes, in these dry noons. The calmness and solitude at office, and a soothing tone in my player, makes me think better and work better. And by work I mean the work I love to do, not those assigned to me by my job.
I am writing this post too on a Sunday noon, from my office. Few years down the line, who knows, looking back on it, I might feel the same about myself - 'you were crazy man - making it to office on weekends!'. But not today.
Subscribe to:
Posts (Atom)