Solving coding puzzles using Mathematics

As I try to study more algorithms, I realize that most of them have their foundation in Mathematics. Without a good knowledge of mathematics, it is not easy to understand algorithms. I guess this is stated in many books, but I doubt if many realize this simple truth. At least, I was not able to realize this truth directly until I made a conscious and consistent effort to learn algorithms a few weeks ago. I find myself often going back to refresh maths basics that I have long forgotten. I also realize that I have been thinking quite the wrong way all this time. Whenever I try to solve a coding puzzle on code chef, I never thought of it as a mathematical problem. I always fantasized and thought about it as a computing problem thinking in terms of traversing all the nodes, sorting data first, etc, etc. by the mid of which, time will have run out and I find myself giving up on the problem. After a month of little reading I did and understanding I had, I now have a different approach on coding puzzles, i.e., the mathematical approach. I now attack the problem from a mathematical stand point first. If you succeed at solving it for one case, then you have pretty much solved the puzzle. You actually succeeded in a computing solution for a single case in a few minutes, which translated into machine code now happens in a few milliseconds or a million in a few minutes.

It is now time to translate your solution to code so that computer can repeat that procedure on a large data set. This is where your knowledge of the language and the computer architecture kicks in. Once you are clear about the algorithm, it is relatively easy to translate that into code compared to fiddling with code all day long not having a clue exactly which step comes next.

Advertisements
Standard

My internet reading

I have learnt programming for the following reasons:

  • It is fun
  • It gives me something to think
  • There is a never ending possibility of many things

I have started about 8 years ago. I didn’t make the best use of all 8 years, but I have constantly tried to stay efficient and I can only say I am still improving. That said, I always missed a mentor or a professional who has already been where I wanted to be. It’s hard to have a great mentor unless you work with some one like that on a regular basis. I didn’t have some one like that all this time, but I did manage to become better, thanks to the internet. I have made internet my guru, only that all the information is spread around the internet in bits and pieces and that it’s a very tedious job to find such pieces. Internet has billions of links and the number only grows every minute. At the same time, the amount of content is so overwhelming that even if you manage to find great stuff, you can’t keep up with every bit of it. There are a multitude of topics and it is overwhelming to keep up with all of them. Being a computer technology guy, I am interested in all the following topics

  • Efficient coding
  • Algorithms
  • New technologies
  • Artificial intelligence
  • Data storage
  • Design
  • Design patterns
  • Performance
  • Scaling
  • Hardware
  • Graphics
  • Games
  • Frameworks
  • Engines
  • Platforms
  • APIs
  • Services
  • Tools
  • etc,etc,etc

And that’s a never ending list of them. And at any minute, there are tons of articles, blog posts, papers, videos about the same topic. It’s nothing but overwhelming. The are few logical choices

  • To balance out on all the topics
  • To pick one topic and stick to it

Both are hard. You can’t ignore any of the topics because they are all so interesting and they are all so important for many reasons. However, time is limited and we have to spend it wisely. So, it is always good to figure out where you want to spend your time and start to let a few things go. In the beginning I didn’t even categorize my reading, I would read anything that came my way.

For example, take sites like Engadget and TechCrunch:

http://www.engadget.com/

http://techcrunch.com/

They publish a lot of new articles every day. They are all about new technologies and the companies behind them. They report the stories behind all these companies and the technologies behind them. You can go about reading them all, and by the time you finish, there are more articles to be read. Sure, you will know about a lot of things like which phone has what features, which company sued which company, which crazy product is being over hyped and made by which company. You’ll feel all techie and hippie and most up to date. But you’re not in the hardware business or in the news business, are you? Where does all this knowledge help you in coding the next big app you have in your mind? The point is that they have tech in their name, but they’re always about the end product and its result. When you want to build a product, you don’t want to know only about the end product – you will want to know about about all the nuts and bolts that make that application. Now, comes the question – where do I find that kind of content?

If you were going around all these articles like me, learning nothing that would help your goal or career, you would be lost in the sea. Luckily for me, my colleague at work pointed me to a great source where I can find about the nuts and bolts, Hacker News. This site has articles posted by members of the community, from various sources and depending on the content, they get voted up and down. This made sense, and I started to read a lot of good articles ranging from new platforms, success stories, people behind them, coding techniques, war stories, motivation, right or wrong, etc. My outlook increased and I moved away from the other sites(though I visit engadget for their in depth reviews of phones/tablets, etc. before I buy them or recommend them for some one). Most of the links you’ll find on HN are first hand experiences from people who have done things rather than the third hand stories written by the staff of those big technology blogs. The comments made in HN(short for Hacker News) will always give me an idea about what people think about these kind of articles and many a times I get surprised by the article reported and what has been published in the story.

Another great resource is Stack Overflow. Though I use it to post my questions, it provides me as a great resource to find great threads that give a lot of insight about any topic. Recently, I have learnt more about Garbage collection in Java, the next thing I did was to search the questions with most votes on tags [java][garbage-collection]. That gives me the best questions asked about these topics and the answers.

Another great resource is Reddit. Of course, the main site consists of a lot of other items you might or might not be interested in, but you’d be surprised to know that there are a multitude of sub reddits on each of the topics you are interested in. It is similar to hacker news, but in reddit, you can deep dive into topics you want. For example, if you want to learn only about Android, you have android and androiddev sub reddits. For bigdata, there is a subreddit. There are one for each of the main topics, and if you don’t find one, you can create one. I haven’t reaped the fruits of reddit yet, but I’m making it a habit to go to these forums regularly. I love reddit.

Standard

If I have seen further it is by standing on ye shoulders of Giants.

When we start our life, we start with zero knowledge. We start to learn by observing anything within our reach. Through our life time, we amass a lot of knowledge. We do a lot of experiments, we try a lot and we fail and succeed some times. Then we die. But life continues. The ones that are born after us still start from zero and go through the whole thing again, making the same mistakes, succeeding some times. If you repeat the last 7 sentences again and again and again, it can be figured out that we don’t need to repeat the same mistakes again. A lot of churn can be cut. So, we capture the knowledge and pass it on. The ones picking that knowledge will have a speed boost and a highway to reach where you left off. So, we need to study and understand all that was done for us. And to advance, we need to start from where it was stopped and take it further giving the opportunity for the next generation to start on it. Let us study.

“If I have seen further, it is by standing on ye shoulders of Giants.”

If I have seen …

Quote