Algorithm Questions

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

Algorithm Questions

Thought Process:
 Try the brute force method
 Use data structures such as stack, queue, linked list, hashmap, binary tree
 Try sorting to solve problems
 Use << operator for multiplication
 Sum of 1 to N integers is n (n+1)/2
 To reverse digits, divide by 10 and print the modulus
 Traverse lists with twice speed to detect loops

1. Given an array of characters which form a sentence of words, give an efficient algorithm to
reverse the order of the words (not characters) in it
 Initialize two counters, front and back, to the end of the input string
 While we haven’t reached the start of the string, move down front until we encounter a
space. Copy the part of the string from this point until back into the output.
 Move back down by one character to account for the space. Set front to back, and repeat till
front is more than 0.
Alternative versions:
Push each word into a stack, checking for spaces. Then pop all of them and add spaces for each
word.

2. Given an array of size N in which every number is between 1 and N, determine if there are any
duplicates in it. You are allowed to destroy the array if you like
 Sort the array in ascending order and then run through one pass of the array comparing of
any element is equal to the next one.
 Create a bit vector/array of size N and set the bit of the index to true when the number is
encountered. If you read a bit which is already set, then duplicates are present. (This will
not work for any arbitrary numbers, other than 1 to N)

3. Give a fast way to multiply a number by 7


Multiply by 8 (left shift by 3 bits) and then subtract the number.
(x << 3) – x
Left shift can be used to multiply a number by the power of 2. The number is the power of 2.
Since 2 power 3is 8, left shift multiplies a number by 8.

4. A real life problem - A square picture is cut into 16 sqaures and they are shuffled. Write a
program to rearrange the 16 squares to get the original big square.
This to me sounds like a sorting problem, you have something that is out of order and you pust put it in
order. Assuming you just have some way of knowing wheter a guess is correct or not, for example:

bool IsCorrectPlacement( Square square, int position );

then you basically have to do an insert sort like this (because there is no partial ordering to exploit for a
better algo like heapsort) which runs in O(n^2):
5. Give me an algorithm and C code to find the subarray with the largest sum given an array
containing both positive and negative integers
Keep two sums – Sum until here and subarraySum
For each element in the array
Add the element to sumuntilhere. If sum is greater than subarraySum, then set subArraySum =
sumUntilHere.
If sumUntilHere is –ve number, then set sumUntilHereto 0

6. Write a function that returns the factorial of a number


Example 1 shows the iterative and recursive solutions. Notice that in both solutions, I check the
input values and boundary conditions. Factorials of negative numbers are undefined, and the
factorial of both 0 and 1 are 1

7. Write a function that computes the nth number in the Fibonacci sequence
The iterative version maintains variables to hold the last two values in the Fibonacci sequence,
and uses them to compute the next value. Again, boundary conditions and inputs are checked.
The 0th number in the Fibonacci sequence is defined as 0. The first number in the sequence is 1.
Return -1 if a negative number is passed.
The recursive version of the Fibonacci function works correctly, but is considerably more
expensive than the iterative version. There are, however, other ways to write this function
recursively in C that are not as expensive. For instance, you could maintain static variables or
create a struct to hold previously computed results.

8. Swap two integers w/o using extra memory


a=a+b;
b=a-b;
a=a-b;

9. Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and
finally create a clean S2 with the relevant characters deleted
We could iterate over the remove string and for each character, remove all occurrences from the
input string. Give input string of length ‘n’ and remove string of length ‘m’, this brute force
algorithm would lead to a complexity of O(mn).
Here’s a more efficient algorithm. Iterate over the remove string, and store each character in an
array. Later as we iterate over the input string, we lookup this array, and if that character is
present in this array, then we remove it from the input. This algorithm works if the array lookup
is a constant time operation. The net complexity of this algorithm would be O(m+n), much better
than the O(mn) brute force algorithm described earlier.
The detailed steps are as follows:
 Create an array ‘flags‘ of 256 characters. Entries in this array serve as flags denoting whether
character ‘i’ is to be removed or not. Initialize all elements of this array to false.
 Iterate over the remove string. For each character i in the string, mark flags[i] as true
 Iterate over the input string. For each character in the string, if flags[i] is true, then copy it to
an output array. Otherwise continue to the next character.
 At the end of this iteration, the output array will contain all characters of input minus all
letters of remove.

10. Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5,
5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9)
Compare element to next element and if not equal, extract it out.

11. Give a one-line C expression to test whether a number is a power of 2


if (x && !(x & (x-1)) == 0)

12. Give a very good method to count the number of ones in a 32 bit number. (caution: looping
through testing each bit is not a solution).
Run a iterative loop.
While n,
Count = count + n & 0x1u;
N >> 1;

Alternative:
While (n)
Count++;
N = n & (n – 1);

13. How can you print singly linked list in reverse order? (it's a huge list and you cant use recursion)
Reverse the pointers till you reach the end and print-and-reverse as you return
while ( loop through list )
{
*List = temp1; //set the head to last node
temp2= temp1->pNext; // save the next ptr in temp2
temp1->pNext = temp3; // change next to privous
temp3 = temp1;

14. How can you find out if there is a loop in a very long list?
One possible answer is to add a flag to each element of the list. You could then traverse the list,
starting at the head and tagging each element as you encounter it. If you ever encountered an element
that was already tagged, you would know that you had already visited it and that there existed a loop
in the linked list.
 Start with two pointers ptr1 and ptr2.

 Set ptr1 and ptr2 to the head of the linked list.

 Traverse the linked list with ptr1 moving twice as fast as ptr2 (for every two elements
that ptr1 advances within the list, advance ptr2 by one element).

 Stop when ptr1 reaches the end of the list, or when ptr1 = ptr2.
If ptr1 and ptr2 are ever equal, then there must be a loop in the linked list. If the linked list has no
loops, ptr1 should reach the end of the linked list ahead of ptr2
15. You are given a list of n numbers from 1 to n-1, with one of the numbers repeated. Devise a
method to determine which number is repeated
The sum of the numbers 1 to n-1 is (n)(n-1)/2. Add the numbers on the list, and subtract (n)(n-1)/2.
The result is the number that has been repeated

16. Insert an item into a sorted list


If item is less than the 1st element, insert at beginning.
If item is greater than the last element, than insert at the end
If not, do a binary search, find the value at middle of the list. If higher, then find the value from
middle unto end. That way, find the position, where the value can be added.
Do a System.arraycopy to modify the array, add the length and copy the correct values.

17. Delete an element from a singly linked list.


We need to handle the case, where the element is the first one. In this case, remove and update
the beginning of the list to the 2nd element.
If the element to be deleted is in the middle, or at the end, then do the following:
 Write an iterative function or a recursive function
 Loop through the list and find the element to be deleted
 While looping keep a reference of the previous pointer
 When element is found, get the reference to the next pointer
 Remove the element, update the previous pointer to the next pointer

18. Write a function to find the depth of a binary tree.


This can be implemented by a simple recursive function:
Write a function findDepthOfTree, which takes a node as the input. Inside the method return
max of FindDepthOfTree (left node), (right node) + 1;

19. Given an array of characters. How would you reverse it. ? How would you reverse it without
using indexing in the array
20. Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if
there is any element in the second list that is an element of the first list (fixed list)
Put the first list in a hashmap, with the key as the number and the value as -1. For each number
in the second list, call contains in the hashmap.

21. How would you find a cycle in a linked list? Try to do it in O(n) time. Try it using a constant
amount of memory
22. Implement an algorithm to reverse a doubly linked list
23. Write a function and the node data structure to visit all of the nodes in a binary tree. (Breadth
first traversal)
Level by level traversal is known as Breadth-first traversal. Using a Queue is the proper way to do this. If you
wanted to do a depth first traversal you would use a stack.

First, the root (1) would be enqueued. The loop is then entered. first item in queue (1) is dequeued and
printed. 1's children are enqueued from left to right, the queue now contains {2, 3} back to start of loop first
item in queue (2) is dequeued and printed 2's children are enqueued form left to right, the queue now contains
{3, 4} back to start of loop

24. Write a function to print all of the permutations of a string.


25. Delete an element from a doubly linked list.
26. How would you implement a queue from a stack?
By using two stacks. Push everything onto the stack. Then for a get, pop everything into a stack and
then do a pop.

27. Write a routine to reverse a series of numbers without using an array.


Void reversenum(int i)
If (I == 0) return;
Print(i%10);
Reversenum((int)i/10);
ex: if the number is 78654
this prints 4 and recursively calls reversenum on 7865, then prints 5 and recursively calls reversenum on
786 and so on

28. Write a bubble sort algorithm.


29. Implement a linked list. Why did you pick the method you did?
30. Implement an algorithm to insert a node into a circular linked list without traversing it.
31. Write a function to print the Fibonacci numbers
if ( n < 2 )
return 1;
for ( i = 1; i < N; i++)
{
f = f1 + f2;
f1= f2;
f = f1;
}

32. How would you write qsort?


33. If you had a million integers how would you sort them and how much memeory would that consume?
Binary tree - requires 1,000,000 integers x 4 bytes = 4 MB + left pointer and right pointer = 12 MB
Insertion sort - can be done in under 4 MB

34. Implement division (without using the divide operator, obviously).


35. What method would you use to look up a word in a dictionary?
36. Compute all prime numbers less than a specified number N
We use an algorithm known as the Sieve of Eratosthenes to solve this problem. How it works is that
for each prime number starting from 2, we mark all multiples of that number as non prime. At the
end of this process, all the prime numbers are identified
Here’s the detailed steps.
1. Create an array of n items, and mark all values as 1 to mark them as prime.
2. Iterate over the array going from i = 2 to sqrt(n). If i is prime, mark all multiples of i as 0 (non
prime)

37. Given an integer value, convert it to its string representation


 Determine if the number is negative. If yes, multiply by -1 to make it positive. Lets call this ‘n’.
 Compute n%10 (remainder of the division). This is the value of the current digit, starting off
with the least significant one. Store this digit in a temp array location.
 Divide n by 10. Assign this to n.
 Repeat steps 2 and 3 till n becomes 0

38. You have an unsorted array of some million random positive integers with no duplicates. Find two
integers from this array which add up to 50
Search for values less than or equal to 50. Suppose your value read is x. If x<= 50, store (key:50,value:x-50) into a
hash table.
Suppose you have the following array..70 60 20 35 90 30 20
for 20, the table will have (20,30).
table.getValues(20) gives you 30. table.getValue(30) is null.
for 35, insert (35,15)
table.getValues(35) gives you 15. table.getValue(15) is null.
for 30, insert (30,20)
table.getValue(30) gives you 20.
table.getValue(20) gives you 30. These give you 50.

39. Given a link list of chars find if the string is a palindrome w/o using extra memory

 Reverse everything from the middle to the end of the linked list O(N/2).

 Use two pointers. One pointing at the beginning and one at the middle then start comparing as
you are incrementing. If they are not equal, then it's not a palindrome. O(N/2).

 Then reverse the linked list from the middle to the end again. This would put the linked list back
to its original state. O(N/2)

Total complexity O(N)


To determine the midpoint without knowing the length of the list, Keep 2 pointers at the head. One moves at
step 1 and other 2 steps at a time. When the faster one falls of the end the slower one will be at the center.

40. Write a function to find the nth item from the end of a linked list in a single pass.
1) have two ptrs. F_Ptr & S_Ptr.
2) Let the ptrs point to the START.
3) Take n as the offset.
4) Start traversing the S_Ptr after the F_Ptr after n.

suppose u want to find 5 element from the last of the list.

In the above case.

1) n = 5 // 5th element from the last


2) check atleast this many elements are there in the list before beginging

// traverse the F_Ptr offset


for(;n!=0;n--,F_Ptr->next);
// now set the S_Ptr to trail the F_Ptr
S_Ptr = F_Ptr;
for(;F_Ptr!=NULL; S_Ptr=F_Ptr; F_Ptr=F_Ptr->next)

Now when F_Ptr = Null, S_Ptr is just trailing behind right! it will be pointing to the n'th element
from the last.

When F_Ptr reaches end (i.e., F_Ptr is NULL)


S_Ptr points to the 5th element from the last.

41.
42.

Brain Teaser Questions


1. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size
or orientation), how would you cut the remainder of the cake into two equal halves with one
straight cut of a knife ?
The cut (plane) should pass through the center of the two rectangles: the outer cake and the
inner hollow area. Since any plane passing through the center of the cuboid will divide the
volume in two halves, both the cake and hollow area are divided into two equal halves.

2. How many points are there on the globe where by walking one mile south, one mile east and
one mile north you reach the place where you started
The answer is "many points". The set of such points is given as {North pole, special circle}.
From north pole, walking one mile south followed by one mile east still keeps you one mile south
of North pole.
The special circle consists of a set of points defined as follows. Let's say you were to locate a spot
near the South Pole where the circular distance "around" the Earth's North-South axis is 1 mile. The
path of such a journey would create a circle with a radius of approximately 840.8 feet (because
C=2.r.pi). Call this point X. Now consider another point Y one mile north of X. The special circle is
the circular path around North-South axis going through Y. If you begin you journey from any
point (say Y1) on this special circle, and travel one mile south, you get to a point (say X1) on the
circle of point X. Now one mile east will bring you back to X1, because circumference of circle of X
is 1 mile. Then one mile North brings you back to Y1. (Answer supplied by Kristie Boman)

3. There are 3 ants at 3 corners of a triangle, they randomly start moving towards another corner..
what is the probability that they don't collide
All three should move in the same direction - clockwise or anticlockwise. Probability is 1/4.
Total no. of movements: 8 A->B, B->C, C->A A->B, B->A, C->A A->B, B->A, C->B A->B, B->C,
C->B A->C, B->C, C->A A->C, B->A, C->A A->C, B->A, C->B A->C, B->C, C->B
Non-colliding movements: 2 A->B, B->C, C->A A->C, B->A, C->B
(i.e. the all ants move either in the clockwise or anti-clockwise direction at the same time)
P(not colliding) = 2/8 = 0.25
4. If you are on a boat and you throw out a suitcase, Will the level of water increase
5. There are 4 men who want to cross a bridge. They all begin on the same side. You have 17
minutes to get all of them across to the other side. It is night. There is one flashlight. A
maximum of two people can cross at one time. Any party who crosses, either 1 or 2 people,
must have the flashlight with them. The flashlight must be walked back and forth, it cannot be
thrown, etc. Each man walks at a different speed. A pair must walk together at the rate of the
slower mans pace.
Man 1 (1 min), Man 2 (2 min), Man 3 (5 min), Man 4 (10 min)
1 and 2 cross together. 1 comes back. then 3 and 4 cross. 2 comes back. then 1 and 2 cross. Total time
is 2+1+10+2+2 = 17
6. You have 5 jars of pills. Each pill weighs 10 gram, except for contaminated pills contained in
one jar, where each pill weighs 9 gm. Given a scale, how could you tell which jar had the
contaminated pills in just one measurement?
Take one pill from first, two from second, three from third and so on. Total pills are n(n+1)/2 and
should weigh 10n(n+1)/2. If it weighs x gm less than that then the x'th jar is contaminated, since we
took x pills from that jar which weighed 1 gm less.

7. One train leaves Los Angeles at 15 MPH heading for New York. Another train leaves from New
York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves
from Los Angeles at the same time as the train and flies back and forth between the two trains
until they collide, how far will the bird have traveled?
If distance is X miles between NY and LA, then it takes X/(15+20) hours for the trains to collide,
and bird will have travelled 25X/(15+20) = 5X/7 miles in that time.

8. Imagine that you have 26 constants, labelled A through Z. Each constant is assigned a value in
the following way: A = 1; the rest of the values equal their position in the alphabet (B
corresponds to the second position so it equals 2, C = 3, etc.) raised to the power of the
preceeding constant value. So, B = 2 ^ (A's value), or B = 2^1 = 2. C = 3^2 = 9. D = 4^9, etc., etc.
Find the exact numerical value to the following equation:
(X - A) * (X - B) * (X - C) * ... * (X - Y) * (X - Z)
Answer is 0, because (X-X) is present in the product

9. You have 12 balls. All of them are identical except one, which is either heavier or lighter than
the rest - it is either hollow while the rest are solid, or solid while the rest are hollow. You have
a simple two-armed scale, and are permitted three weighings. Can you identify the odd ball,
and determine whether it is hollow or solid.
10. Write an efficient algo and C code to shuffle a pack of cards.. this one was a feedback process
until we came up with one with no extra storage.
11. A real life problem - A square picture is cut into 16 sqaures and they are shuffled. Write a
program to rearrange the 16 squares to get the original big square.
12. Given a maze with cheese at one place and a mouse at some entrance, write a program to direct
the mouse to cheese correctly. (Assume there is a path). Following primitives are given:
moveforward, turnright, turnleft, iswall?, ischeese?, eatcheese.
13. Give an algorithm that calculates the distance between two text strings (only operations you can
have are: delete, add, and change, one by one)
14. Given the definition of a sequence (5 4 7 6 is, but 1 2 4 5 is not), write an algorithm to check if an
arbitrary array is a sequence or not. Once I figured out a solution, I was asked to do a space and
time complexity analysis
15. Given the time, devise an algorithm to calculate the angle between the hour and minute hands
of an analog clock.
The important realization for this problem is that the hour hand is always moving. In other
words, at 1:30, the hour hand is halfway between 1 and 2. Once you remember that, this
problem is fairly straightforward. Assuming you don't care whether the function returns the
shorter or larger angle, Example 4 shows a solution to this problem

16. Given an eight-bit bitmap graphics file, devise an algorithm to convert the file into a two-bit
ASCII approximation
17. Given a history of URLs, how would you determine if a particular URL had been seen before?
18. Given ships travel between points A and B, one every hour leaving from both ends
(simultaneously), how many ships are required (minimum), if the journey takes 1hr 40 mts.
How many ships does each ship encounter in its journey, and at what times?
4, 3 at 20 mts, 50 mts and 80 mts.
19. Given a number, describe an algorithm to find the next number which is prime.
20. How many golf balls can fit in a school bus?
21. Every man in a village of 100 married couples has cheated on his wife. Every wife in the village
instantly knows when a man other than her husband has cheated, but does not know when her
own husband has. The village has a law that does not allow for adultery. Any wife who can
prove that her husband is unfaithful must kill him that very day. The women of the village
would never disobey this law. One day, the queen of the village visits and announces that at
least one husband has been unfaithful. What happens?
22. In a country in which people only want boys, every family continues to have children until they
have a boy. if they have a girl, they have another child. if they have a boy, they stop. what is the
proportion of boys to girls in the country?
Pretty simple. Half the couples have boys first, and stop. The rest have a girl. Of those, half have a boy
second, and so on.
So suppose there are N couples. There will be N boys. There will be an "infinite" sum of girls equal
to N/2 + N/4 + N/8 + ... As any college math student knows, this sum adds up to N. Therefore, the
proportion of boys to girls will be pretty close to 1:1, with perhaps a few more boys than girls
because the sum isn't actually infinite.

23. If the probability of observing a car in 30 minutes on a highway is 0.95, what is the probability
of observing a car in 10 minutes (assuming constant default probability)?
24. If you look at a clock and the time is 3:15, what is the angle between the hour and the minute
hands? (The answer to this is not zero!)
25. You are at a party with a friend and 10 people are present including you and the friend. your
friend makes you a wager that for every person you find that has the same birthday as you, you
get $1; for every person he finds that does not have the same birthday as you, he gets $2. would
you accept the wager?
26. You have five pirates, ranked from 5 to 1 in descending order. The top pirate has the right to
propose how 100 gold coins should be divided among them. But the others get to vote on his
plan, and if fewer than half agree with him, he gets killed. How should he allocate the gold in
order to maximize his share but live to enjoy it? (Hint: One pirate ends up with 98 percent of the
gold.)
27. You’ve got a 10 x 10 x 10 cube made up
of 1 x 1 x 1 smaller cubes. The outside of
the larger cube is completely painted red.
On how many of the smaller cubes is there
any red paint?
28. First, note that the larger cube is made up of 1000 smaller cubes. The easiest way to think about this is how
many cubes are NOT painted? 8 x 8 x 8 inner cubes are not painted which equals 512 cubes. Therefore, 1000
– 512 = 488 cubes that have some paint. Alternatively, we can calculate this by saying that two 10 x 10 sides
are painted (200) plus two 10 x 8 sides (160) plus two 8 x 8 sides (128). 200 + 160 + 128 = 488.
29.
30.

1. Introduction
The technical interview is perhaps the most intimidating and mysterious event that job hunters
experience in the quest for that "killer offer." The rigor and format of the technical interview
varies from one company to the next, but there are some fundamental similarities. An interviewer
presents you with a problem to be solved. The interviewer may leave the room and give you
some time to work the solution out before returning. Or the interviewer may wait patiently while
you study the problem and figure it out. The interviewer may even start quizzing you right away
about aspects of the problem and approaches to solving it. Some of these problems can appear
quite challenging, especially if you've never been through a technical interview before. To make
matters worse, simply getting the answer to the problem may not be enough to land the job. On
the other hand, getting the correct answer may not even be necessary. What is an interviewer
looking for in candidates during the technical interview? Interviewers recognize that this setting
is, by its very nature, stressful. Otherwise competent candidates may be completely stumped by
an interview problem only to discover an elegant, simple solution later that night. Interviewers
may be interested in seeing how you work under stressful situations or how well you adapt. It is
worth noting that interviewers are more interested in seeing how you work than seeing whether
you can come up with the correct answer. In this article, I will deal with both how you can better
showcase your skills and experience, and what kinds of problems you can expect to be asked.

2. The Basic Rules


These basic rules are often taught to programmers and are (or at any rate, should be) drilled into
your head in computer-science classes. For some reason, however, they are easily forgotten
during the technical interview. Being one of the few candidates careful and experienced enough
to remember these important steps can make the difference between getting an offer and getting
the cold shoulder. Don't be afraid to ask for clarifications of the problem or the requirements for
the solution.

3. Don't assume all data is given to you.


You should never assume that you have been given all the data necessary to solve the problem to
the satisfaction of the interviewer. This is especially likely to be the case when interviewing with
IT consulting companies. In this environment, the client may need some prodding in order to
provide a complete specification. So, the reasoning goes, ideal candidates will be willing to talk
to the client to figure out the expected inputs, the desired outputs, the data ranges and data types,
and the special cases. The ideal candidate will ask these questions rather than spend all the
allotted time coming up with a solution that doesn't meet the client's needs. The first thing to do,
then, is to make sure that you and the interviewer agree on what the problem is and what an
acceptable solution would be. Make all of your assumptions explicit and up-front, so the
interviewer can correct you if you are mistaken.

4. Think out loud.


If the interviewer stays in the room after presenting the problem, he or she is interested in seeing
how you analyze and approach a problem. Of interest are how possible solutions are considered
and eliminated. And frankly, watching a candidate sit and stare at a problem isn't all that
entertaining for the interviewer. Always allow sufficient time for design. The worst thing that
you can do while attempting to solve a technical problem is to dive right into coding a solution
and get about half way through it before realizing that the approach is all wrong. This is where a
little forethought can save a great deal of effort and embarrassment. Don't worry about running
out of time to answer the question before finishing the code for the solution. The idea, the
algorithm, and the approach are the most important elements here. If you're having trouble
writing the code, offer to explain the algorithm. Stress and anxiety can make the technical
interview more difficult than it needs to be. If you find yourself having difficulty with
programming syntax or constructs, you should make sure that the interviewer knows that you
understand the problem and its solution. While it's best to get both the algorithm and the
implementation correct, it's far better to get points for demonstrating facility with one than fail to
demonstrate either. Be prepared to identify bottlenecks, possible optimizations, and alternative
algorithms. Just because you've found one solution that produces the correct output, doesn't mean
the problem has been solved to the interviewer's satisfaction. Interviewers, hinting at possible
improvements, may prod you at this point. Occasionally, you may take an approach that the
interviewer didn't anticipate. At this point, an interviewer may ask you to take a more
conventional approach. This doesn't mean that you've done anything wrong; very often, an
interviewer may be leading you along a particular approach with a purpose in mind. The
interviewer may be intending to ask follow-up questions or present new problems that build on a
particular solution.

5. Good programming practice.


Initialize all variables, give variables descriptive names, and always use comments.
Interviewers may be watching your solutions to determine whether you follow good
programming practices. Good programming practices make it easy to understand other people's
code. This means that there aren't cryptic variables, functions with undocumented side effects,
obfuscated algorithms, and sloppy (read: buggy) code. Just because you are being interviewed
(and therefore, coding on a whiteboard or on a piece of paper) doesn't give you an excuse to be
sloppy or lazy. Commenting code for an interview may seem like a waste of time, but some
interviewers do look to see that candidates write comments while coding or before coding, rather
than adding them in as an afterthought.

6. Check all boundary conditions.


Candidates forget to do this frighteningly often. In fact, practicing programmers sometimes
forget to do this. That's how bugs get started. You should verify that your code properly handles
cases where inputs are negative or zero, lists are empty, strings are empty, and pointers are
NULL. This is also a good habit to have after you get the job. Expect bad input from users. Users
rarely do as they are expected. You should protect your code, and return a descriptive error back
to the user. Display enthusiasm. Don't underestimate the importance of your appearance and
attitude during the interview. While your skills and experience may be the focus of the technical
interview, looking bored or uninterested may do more to sabotage your chances than blowing an
interview problem.

7. Work Things Into Your Conversations


In addition to these basic rules for the technical interview, there are some other things worth
pointing out. Interviewers don't always have the chance to examine your résumé in advance.
This means that the interviewer may not be aware of your past work experience. Don't hesitate to
point out experiences working in teams (whether as a part of a past job, a class programming
project, or a hobby), working on large projects (paying attention to time spent on design,
implementation, and testing), dealing with customers to define requirements, and managing
people and projects. Interviewers are interested in hearing about successes as well as failures.
When these past experiences weren't successful, you should point out the lessons learned or
wisdom gained as a result of these failures. Interviewers want to see that candidates who have
had negative experiences are not going to repeat their mistakes.

8. Typical Technical Questions


When preparing for a technical interview, you should review basic structures (linked lists, binary
trees, heaps) and algorithms (searching, sorting, hashing). Having a mastery of these topics will
likely give you all the necessary knowledge to tackle the problems you will encounter during the
technical interview. Also, review the areas for which you're interviewing. If you're interviewing
for a systems programming job, review the differences between threads and processes, OS
scheduling algorithms, and memory allocation. If you're interviewing for a job that requires
experience with an object- oriented language, spend some time brushing up on object-oriented
methodology.

You might also like