Pages

Friday, 27 June 2014

Algorithm Interview Questions



1. How can you retrieve the center character of an array of strings?
First, as you know, an array can have any number of indexes, so the array can be a variable size. Most languages let you sort a string and convert the entire array to a string. The sort function is beneficial when you want to get the center character for a sorted list of strings. The first step would to sort the array, and the second step is to convert the array to one long string. It might help to ask the interviewer if blank spaces should be included as they are considered characters or a value in programming languages. After you convert the array to a string, if blank spaces must be removed, you can use the language’s “replace” function to remove blank spaces. Finally, you get the length of the string, divide by 2 and then that position is the center character for the array of strings.

2. How could you write code to reverse a string?
Again, this question is more about how you engineer code. There is no right or wrong answer, and most algorithm questions can be solved multiple ways. Here is one way you can store a string in the reverse order. There is no internal function that lets you reverse a string, so you’ll need to write a loop structure. The first step is to trim the string from any prefixed or appended blank characters. Most languages have a trim function that will do both of these steps for you. The next step is to get the length of the string. From the length of the string, you can then create a for loop that counts backwards from the back of the string to the beginning. In the loop, you create a variable that stores the characters in the reverse order.

3. Create a loop to find a series of Fibonacci numbers.
Fibonacci numbers are a series of numbers where the next number is the sum of the previous two numbers. You can also do this using a loop sequence. You can start with 0 and 1, and the next number would be 1, then the next would be 2, and then the next would be 3. Fibonacci numbers look like the following sequence “0, 1, 1, 2, 3, 5, 8, etc.”
There are several ways you can accomplish this, but one way to do it is to create a loop. You must define up to what number you want to go to or you’ll create an infinite loop. You can use the first two numbers, add them up and store them in a temporary variable. You then re-dimension your array and add the number to the end of the array’s indexes.
In the loop’s termination section, make sure you use the greater than or equal to directive to ensure you include the last number in the sequence.

4. Create a function that determines if a number or string is a palindrome
A palindrome is a string or a number (or a word) that is the same when you print it in reverse. For instance, “mom” is a palindrome or “12321” is a palindrome sequence of numbers.
You can accomplish this task using a loop and then an embedded loop within the loop. You would loop through the sequence and then in the inner, embedded loop, iterate through each character and save the reversed string in a temporary variable. After the embedded loop is finished, you could then compare the two variables to see if they are the same. Most languages have a string compare function that does the actual binary comparison for you.

5. Perform a bubble sort in x language
Bubble sorts are standard in college. Most colleges have you perform a bubble sort in Java or C++, but you could be asked for other languages. You perform a bubble sort using two loops: the first loop compares the first two numbers, and then the second embedded loop goes through the same list of numbers comparing the current number in the list. Bubble sorts typically sort numbers, but you can also sort strings.
http://www.facebook.com/chand2like
 

No comments:

Post a Comment