My Favourite Interview Questions
July 20, 2008
Recently I have been interviewing people for the post of software engineer. We were looking for fresh developers so most of the candidates were students which graduated recently or were graduating in a month or so. Asking all sort of language questions seem very odd for them as they don’t have any professional experience. The criteria I set was to judge how smart they are and then ask them some programming question which they had to implement in their favorite programming language. I don’t check for syntax errors as I said earlier the criteria is to judge how smart they are and how efficient and optimized solution they can come up with. So here are some of the questions I usually ask:
- Given a sorted array of integers of length N containing different integers. The array is sorted in ascending order and can have duplicates. The developer needs to come up with the most efficient (for both speed and memory) solution which returns an array containing integers with duplicates removed.
For example if the array is: int[] array = new int[] {1, 2, 2, 3, ,4, 5, 5, 6} the resulting array will be {1, 2, 3, 4, 5, 6} - Given an array of 1001 elements which contains integers from 1 to 1000 inclusive. The numbers are randomly stored in the array. Only one number repeats itself. The candidate has to come up with an efficient solution for finding that duplicate given that you can access the elements of an array only once i.e., you can read the elements of the array only once.
- This one’s my favorite. I usually ask this to experienced developers in order to judge how good they are at tackling difficult problems. Here is the question. We need to store a family tree in the database. The schema should be able to store the name, sex and relationship of each family member. Based on the schema the candidate produces I ask them to write some queries like:
return all the family members who don’t have any children or return all the children of some family member name that I give. This will include the children of children also. - We want to implement an election algorithm. The input will be an integer N which specifies the number of people from which we need to elect the person. Lets assume that all people are standing in a circle. The algorithm is to eliminate every alternate person and continue in this fashion until we have one person left. The best method for its implementation is to use a circular linked list. The candidate needs to write the code to initialize this linked list and then write the code for the algorithm.
- This is one of the toughest questions I have in my arsenal. The candidate needs to implement the perfect shuffle algorithm. The candidate can use a special function int rand(int n) which returns any number from 0 to (n – 1) inclusive. The implementation of the algorithm uses some sort of swap(int a, int b) function which every one knows uses 3 operations to swap the two numbers. I than ask the candidate to optimize the code by implementing this swap function in two operations. This swap function doesn’t have to be a separate function, and any changes to the whole implementation is OK as long as you can come up with an optimized solution.
- The candidate needs to write a code for reversing a word given as input to the function. The signature of the function is: String reverse(String inputString) After the candidate has successfully implemented the code I ask the real question. Which is to come up with a sentence reverse function which reverses all the words in the sentence i.e., if the input sentence is “The quick brown fix jumped over the lazy dog”, the output will be “dog lazy the over jumped fox brown quick The”. Confused? Just concentrate on how can you use the reverse function to implement the solution.
These are some of the question I usually ask. There are many more that I have. In the next post I will post the solutions that I think are the best but the readers are welcome to post solutions to these problems.
Language shift for better performance
July 5, 2008
I have heard this many times that language X is faster than language Y. Sometimes the comparisions are done between languages of different generations but what worries me the most is when decision of language shift are taken for improving the performance.
Being a Java programmer I have heard the debate of .Net faster than Java and the more old one being C++ faster than Java. All is fine until the language shift decisions are taken just because of performance reasons. It is like you have tried every possible optimization you could think of and the conclusion you get is the current programming language is slow and now you need to re-write in some other languages.
Sometimes people go even further in these comparisions when they start comparing languages of different generations for example people tend to compare Groovy or Ruby with Java wherease both languages are different in their basics where one being a scripting language and other a full blown programming language. What it will be like if I compared Java with assembly? You get the point.
Language decisions should be based on:
How much experience is available in the programming language. Java has the advantage of world’s best brains working on it like Joshua Bloch, Neal Grafter to name a few. Also how much resources are available in the local market.
How fast is it to program in the language. This can be pretty relative measure as experienced programmers can be very very fast in the language they program. But I would like to clarify this with an example. Lets compare Java with PHP, java is a very type safe language where the compiler can warn/notify you of most of the errors whereas PHP being a loosly typed language where you don’t have to decalre the type of variables and not even declare them. I have been programming in Java for almost 7 years now but when I wrote my first project in PHP I was really amazed at how fast I completed the project.
The most important factor about language decision should be the requirements for the task at end. Lets say you want to build a simple reporting application then PHP or .Net would do just good. Similarly for a basic CRUD application scripting languages like Ruby with RoR(Ruby on Rails) would be great. But if you want to build a highly scalable enterprise application that is where you need make good choices. You need to not only consider the factors outlined above but also take a look at the various third party applications/libraries available that can help achieve scalability/availability.
Apart from these factors code mantainability etc. also play a major role in language decisions but in the end you can come up with good frameworks to tackle these problems.
Trust me you can do a lot before you come to the conclusion that the the programming language is the bottle neck. So try hard and you will find solutions, sometimes the solution is under the hand but we fail to realize that.
Before I signoff I would like to make some comments about the people thinking that programming in java is slow. It is not because Java is a very hard language but is because of the amount of choices available for doing a task. Jaava is the only language which has so many web/application frameworks available and for a new comer these many amount of choices creates confusion as to which framework to choose for handling the task. Just compare the java frameworks with .Net, you hardly can name more that three frameworks in .Net. These choices also make life easier for the developers as you dont have to re-invent the wheel every few weeks because most of the time the problem you are facing is faced by someother developer and they have come up with solutions so lets use that. This is true for almost every programming language but is more visible in Java/PHP world because of the amount of developers following the language.
Success - Failure
July 5, 2008
“Success is the ability to go from one failure to another with no loss of enthusiasm.”
Winston Churchill
Disclaimer
July 1, 2008
I felt a strong need for this on my blog. It seems like I am a lot popular than I expected so here it is:
Disclaimer
- This blog, despite what others might think of it, is still a personal blog. Thus, it will reflect my personal opinions and observations.
- The characters and incidents portrayed and the names herein are fictitious and any similarity to the name, character and history of any person, living or dead, is entirely coincidental and unintentional.
- I don’t have an editor. Well, you can call me my own editor but there’s a reason why book authors and newspapers have a separate editor to do the job right? So, expect more typos or grammar boo boos from time to time. Nobody’s infallible. Only request I have to my readers is to point it to me if I do.
- My sources have the right to privacy/anonymity and I will protect that the best I could. A lot of people have tried in the past but failed.
- Sometimes, my reasoning may be flawed but you are welcome to comment or blog about it. That’s the beauty of the blogosphere — you can always join in the conversation. If you keep your silence, then it’s nobody’s fault but yours.
- I may do reportage and I will link or point to the source as properly as I can.
I may write speculations (i.e. rumors) and it will be clear that it’s either my own speculation or of - thers which I lifted it from. These speculation are subject to the “truth test” and anyone is welcome to prove or disprove it.
- I may do opinion pieces and it will be clear that it’s my opinion and of nobody else. Whether or not my opinions are backed up by research, academic papers or authority is all irrelevant.
- I am not obliged to blog everyday. I just do and will always do as long as my fingers are complete and I have internet access.
- I welcome comments. I do not promise I can reply to each one of them in due time but I will try my best. Some comments might be flagged as spam so don’t blame me right away if they do. We can always fix that.
- Lastly, I reserve the right to amend, revise, or add to this disclosure in the future or until someone else hits me with their mumbo-jumbo.
In addition, my thoughts and opinions change from time to time…I consider this a necessary consequence of having an open mind. This weblog is intended to provide a semi-permanent point in time snapshot and manifestation of the various memes running around my brain, and as such any thoughts and opinions expressed within out-of-date posts may not the same, nor even similar, to those I may hold today.
hrhrhrhrhr…
How to debug applications launched using Java Web Start
June 29, 2008
Just set the following parameter:
set JAVAWS_VM_ARGS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8200
where address specifies the port on which you can connect for remote debugging. Then launch your application using javaws yourfile.jnlp
I was talking to a senior java developer sometime back. We were discussing some code base and we can ame across a interresting snippet:
try {
myVar1.getMyVar2().getMyVar3().doSomething();
} catch (NullPointerException npe) {
}
I was amazed to see this line of code and I immediately asked why was this written like this. The experienced java developer said that he put in this line to inorder to fix a NPE occurance QA had reported. upon asking why not put in a simple null check around the variable which was null instead of having a try catach block he said this strategy had several advantages the most attractive of them was then he didn’t have to debug and check the variable which was null. This guarded against all any of the variables involved in the code against NPEs.
I think thisd is not at all a good approach because you are not actually fixing the problem but only hiding its occurance. In some cases putting in a null check is analogus to catching the NPE. I think a proper fix should be to find out the root cause of why we had a nbull reference and fix iut from there. There are sometimes race conditions in the code that can cause these null references and hiding the occurance like this can cauise all sort of problems in the long run.
Numbers are Case-Sensitive !!!
May 5, 2008
Numbers are nevers case sensitive but it does seem from this code
if (input.equalsIgnoreCase("5")) {
// do something
}
Reversing Who Has the Biggest Brain (Part 1)
May 4, 2008
Hi all,
Today I had nothing to do so I decided to reverse engineer this awesome Facebook application called Who Has the Biggest Brain

Some facts about the game first:
It tries to measure the size of your brain by judging the player in four categories using 8 minigames. You get to play 4 mini-games in one game. Each mini-game lasts only 60 seconds. And in the end you get a comnbined score which tells you which brain type you are.
Now lets get into the technical details first. It seems like playfish decided to use the amozon’s Simple Storage Service (http://aws.amazon.com/s3) to host the game. This act can be confirmed by taking a look at the source of page to find out from where the swf file is getting loaded.
First thing to note is that this game can only be played while you are online. So this implies that there is some communication going on between the game and some server which happens to be on the playfish domain.
I tried to get hold of all the communication that is going on between them. And it seems like there are at some places where the game tries to communicate. First thing I noted is that there is no constant communication i.e., athe mini-games that are presented ar not ommunicated through the server. It is all coded in the swf file. There seems to be a communication before the start and at the end of each mini-game.
It has been reported that if you have slow clock the game notifies this as cheat and doesnt saves the score. It can be that this communication is related to the start and end time of each mini-game. So that the server can check the difference between start and end time is with-in a desired threshold othrwise it can be flagged as a cheat. So this rules out the slow clock cheat to get a good score in this game.
Good job done by the developers at playfish. But you can still squeeze out 3-4 seconds and remain in the threshold
Stay tuned for further analysis as I will try to rever engineer this communication to reveal more factsa abut this game.
SQL Cross Join Gotcha
May 4, 2008
I was fixing a bug in our product sometime back. It was related to data migration . As we had revamped the functionality altogether the data fom the previous version of the product wasn’t showing up at all in the new reporting interface.
There were new tables added inorder to implement the new functionality so the reporting queries were also modified so that these tables were taken into account. As I was going through the code I found the specific query which wasn’t returning any results.
The query was a page long so I decided to follow the rule of elimination (gradually removing the conditions from the where clause) to find out the issue. I started removing the conditions and in the end I was left with a simple cross join of 5 tables. But still the query wasn’t returning any data.
Then I started checking the contents of the table and it turned out that the new table added didn’t had any row at all. So the cross join wasn’t returning any data since the last table was empty.
So don’t be surprised when a cross join doesn’t return any data. Make sure that you are not taking a join with an empty table ![]()
Race conditions
February 23, 2008
Its been a long time since I posted. Today I am going to talk about race conditions. So lets start off with the basic question, What exactly is a race condition?
“A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time, but because of the nature of the device or system, the operations must be done in the proper sequence in order to be done correctly.”
Lets elaborate on this with an example. Lets have a example of a simple Map which stores name, value pairs for us. This Map is stored centrally and acts as a cache for us. Whenever we want some value we first check teh Map for teh value if found we return the value as is otherwise we create a new value and insert it into our map and return the created value.
Here is a sample class to achieve the purpose:
public class Cache {
private Map<String, Object> map = new HashMap<String, Object>();
public Object getValue(String name) {
return map.get(name);
}
public void putValue(String name, Object value) {
map.put(name, value);
}
}
and here is a sample usage:
Cache cache = new Cache();
if ( cache.getValue('test'
) == null ) {
Object obj = new Object();
cache.put('test', obj);
}
Quite simple. Is there any possibility of having created two Objects for the value test in our system? If yes then you don’t have to read further you already know what are race conditions but for those of you who aren’t here is the explanation:
Imagin two threads Thread1 and Thread2 running this exact same piece of code. Now both the threads start at the same time. They execution pointer reaches the line 4 where we are querying for an object but since it’s not in the cache both the threads get a null. So they both proceed furhter and we end up creating two Objects in the system. So here is the problem. Two threads are racing for the same resource and that is why it is called a race condition. So how do we fix up the thing?
You must be thinking that making the methods getValue and putValue synchronized should fix up the issue but it isn’t that simple. Because the correct order for execution invloves two operations. When we had a race condition the order of operation was getValue, getValue, putValue and putValue. The correct order should have been getValue, putValue and then getValue. So how do we do that. Well the simplest solution is to make the block in which we are accessing this shared data synchronized. This is also termed as the critical section. In our case the critical section of our code is from line 4 - 10. Wrapping this inside a synchronized block should fix the problem.
Cache cache = new Cache();
synchronized (this)
{
if ( cache.getValue('test'
) == null ) {
Object obj = new Object();
cache.put('test', obj);
}
}
There can be many optimizations that we can do to further improve the solution, more on this later when I talk about the different levels of synchronization that java provides and overheads involved in each one of them. But for now this should be enough to handle the situation.
So as a final thought whenever you have a SHARED resource which can be accessed by m,ultiple threads at the same time always keep the race conditions in mind. These are the simplest of the mistakes that most of the programmers make while coding and these turn out to be the hardest one to fix once the system gets into production. Always keep an eye out for these when coding! Until then happy coding.