27 April 2011
Android SDK - File Not Found
Every time I tried to update the Android SDK on Windows 7 I'd receive a 'Nothing updated' error. Looking through the running log of the download there were multiple File Not Found errors. I ran into this when I tried adding in the nook color emulator URL as well.
26 April 2011
Binary Searches
So a mythological interview question supposedly asked by F______k is, "Given the numbers 1 to 1,000, what is the minimum number of guesses needed to find a specific number if you are given the hint 'higher' or 'lower' for each guess you make."
I cannot provide a citation for that question and have not heard it asked myself. It has been passed all around the internet as a 'weird interview question.' This question is indeed not weird at all and you, the programmer, should be able to know how to address this straight away.
A related question I was asked by M_______t was, "When does one use a binary search?" Let me tell you this, the only answer they want to hear for this question is, "When the data is already sorted." Quite obviously, data from 1 to 1000 is already sorted.
So, lets return to the original question and break it down binary style, yo. Lets assume our number is 815. Our independent calculation guesses as follows:
So, we're looking at around eight or nine iterations to find our answer. Although it could be less, what if the number was 500? Obviously it would hit on the first iteration. The question was, "what is the minimum number of guesses needed to find a specific number" so in a literal interpretation of that question would answer, "one".
Anyway, back to binary searches. There is no need for me to define them here as you can and should be able to find that information out yourself. So on to some code! A representation of the above in C++ would be as follows:
If called with binSearch(815, 1, 1000) our result would be the following:
So there you have it.
I cannot provide a citation for that question and have not heard it asked myself. It has been passed all around the internet as a 'weird interview question.' This question is indeed not weird at all and you, the programmer, should be able to know how to address this straight away.
A related question I was asked by M_______t was, "When does one use a binary search?" Let me tell you this, the only answer they want to hear for this question is, "When the data is already sorted." Quite obviously, data from 1 to 1000 is already sorted.
So, lets return to the original question and break it down binary style, yo. Lets assume our number is 815. Our independent calculation guesses as follows:
- Guess: 500
- Response: higher
- Guess: 750
- Response: higher
- Guess: 875
- Response: lower
- Guess: 812
- Response: higher
- Guess: 843
- Response: lower
- Guess: 827
- Response: lower
- Guess: 819
- Response: higher
- Guess: 815
- Ding
![]() |
| I don't know about you, but this is how I code. |
Anyway, back to binary searches. There is no need for me to define them here as you can and should be able to find that information out yourself. So on to some code! A representation of the above in C++ would be as follows:
int binSearch(int needle, int low, int high) {
if (high < low)
return -1; // no match
int middle = low + ((high - low) / 2);
// added debug output for demonstration.
cout << "middle: " << middle << " low: " << low << " high: " << high << endl;
if (needle > middle)
return binSearch(needle, middle, high);
else if (needle < middle)
return binSearch(needle, low, middle);
else
return middle; // match
}If called with binSearch(815, 1, 1000) our result would be the following:
middle: 500 low: 1 high: 1000
middle: 750 low: 500 high: 1000
middle: 875 low: 750 high: 1000
middle: 812 low: 750 high: 875
middle: 843 low: 812 high: 875
middle: 827 low: 812 high: 843
middle: 819 low: 812 high: 827
middle: 815 low: 812 high: 819So there you have it.
19 April 2011
FizzBuzz Examples
I have noticed lately programmers and interviewers are all up in arms about the FizzBuzz programming task. Interviewers are making a deal of the fact that programmers cannot even write a basic FizzBuzz program, even recent graduates.
First off, this is an absurd trend and non-news in developer interviews. While the argument exists to make developers develop in an interview, often the tasks are absurd and impractical. This is yet another example and part of the reason for my recent series on interview questions.
So on to the question.
Done deal. We're printing out the numbers from 1 to 100. Now onward."But for multiples of three print “Fizz”" Alright remember, how we can find out if something is a multple of three? Hmm? Divide it by 3? Is there a remainder? No? Well it's divisible by 3 then. duh. So use the dreaded mod here.
![]() |
| Where does one buy a domino shirt? |
So on to the question.
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz.” For numbers which are multiples of both three and five print “FizzBuzz.”Seems simple enough right? Don't overthink this one. Let's break it down."Write a program that prints the numbers from 1 to 100." Okay so we need a loop. What kind of loop? Well you can do lots of things here, try and wow them with a recursive loop, use a do..while or a for loop. Let's use the KISS method and go with a for loop.
for (int i=1;i<=100;i++) {
cout << i << endl; }
Done deal. We're printing out the numbers from 1 to 100. Now onward."But for multiples of three print “Fizz”" Alright remember, how we can find out if something is a multple of three? Hmm? Divide it by 3? Is there a remainder? No? Well it's divisible by 3 then. duh. So use the dreaded mod here.
for (int i=1;i<=100;i++) {
if (i % 3 == 0)
cout << "Fizz"<< endl;
else
cout << i << endl;
}
Alright now I think you have this, continue on in this method and check if it's divisible by 5...
for (int i=1;i<=100;i++) {
if (i % 3 == 0)
cout << "Fizz"<< endl;
else if (i % 5 == 0)
cout << "Buzz"<< endl;
else
cout << i << endl;
}
Oops! It seems that we've backed into a corner and missed the final step of requirements. For numbers which are multiples of both three and five print “FizzBuzz.”. Just by eliminitating the else if and the line breaks we should have it then, right?
int main() {
for (int i=1;i<=100;i++) {
if (i % 3 == 0)
cout << "Fizz";
if (i % 5 == 0)
cout << "Buzz";
if (i % 3 != 0 && i % 5 != 0)
cout << i;
cout << endl;
}
}
"But I can write this so much more elegantly!" You say. Please do. That is what will separate you from the rest of the pack.
08 April 2011
C++ Proficiency - Palindromes
Write a boolean in C++ to detect if a string is a palindrome.
This was a question asked in a M_______t technical interview. For the life of me, I couldn't recall what a palindrome even was at that time. The example given was level.
Around line 2 one would want to add some replace functions to remove non-alpha characters and spaces. Think about the common example used "dammit i'm mad". It will return false if given to this function as is but is truly a palindrome.
I think a better bool function would be to detect if a string is a Palinism. In other words, if the coherency of the sentences are random babble, mixed with n number of keywords, add in some further nonsense, it returns true.
This was a question asked in a M_______t technical interview. For the life of me, I couldn't recall what a palindrome even was at that time. The example given was level.
1: bool isPalindrome(string test) {
2:
3: for (int i=0; i < test.length() - 1;i++){
4: if (test[i] != test[test.length() - 1 - i]) return false;
5: }
6:
7: return true;
8: }Around line 2 one would want to add some replace functions to remove non-alpha characters and spaces. Think about the common example used "dammit i'm mad". It will return false if given to this function as is but is truly a palindrome.
I think a better bool function would be to detect if a string is a Palinism. In other words, if the coherency of the sentences are random babble, mixed with n number of keywords, add in some further nonsense, it returns true.
06 April 2011
Java Proficiency - Access control
How can one make a certain method in a certain class only accessible by classes that are defined within the same package as the class of the method? How do you enforce this restriction?
As with the other technical interview articles, these are meant to be a quick reference for an answer rather than a tutorial on use.
- mark the method "public"
- Right here, you know this one is wrong. Public is accessible by all methods in all packages.
- mark the method "package"
- This isn't even a modifier that exists. So pass. One might be temted by this answer and it is meant to throw you. Know your Java access modifiers.
- mark the method "protected"
- Well this is closer to restriction and in a sense true, however, protected is accessible by the class only and the subclasses of the class.
- do not mark the method with a modifier
- Otherwise known as default. This is the answer we're looking for. Providing no modifier makes the method accessible throughout the same package only.
As with the other technical interview articles, these are meant to be a quick reference for an answer rather than a tutorial on use.
C++ Proficiency - macro function
Define a macro function in C++ that takes parameters a and b and returns which one is greater.
How would you use the function in your program?
1: #define WhoIsGreater(a, b) \
2: ( a > b ? a : b )
How would you use the function in your program?
1: int main() {
2: cout << "Between 3 and 5 the greater is: " << WhoIsGreater(3,5) << endl;
3: }
Again this is not a tutorial on how to make macro functions, it's a quick refresher and an actual question asked in a technical interview.
Java Proficiency - final
What does the keyword final do in this code block?
Methods declared as final within a class cannot be overridden by a subclass. So if we extend MyClass we can't be messin' with doStuff(). It's final.
What if the class is declared as final?
Declaring a class as final means that class cannot be extended.
These short answers are not meant to be a definition or a tutorial in the use of final. They are meant to give the answer the technical interviewer seems to want.
1: public class MyClass {
2: public final int doStuff() {}
3: }
Methods declared as final within a class cannot be overridden by a subclass. So if we extend MyClass we can't be messin' with doStuff(). It's final.
What if the class is declared as final?
1: public final class MyClass {
2: public int doStuff() {}
3: }
Declaring a class as final means that class cannot be extended.
These short answers are not meant to be a definition or a tutorial in the use of final. They are meant to give the answer the technical interviewer seems to want.
SQL Joins Demystified
Explain the types of joins in SQL.
Yet another of those common technical interview questions that every interviewer wants to know, every candidate gets wrong and everyone looks up anyway when using them. There are four types of joins that this article focuses on Inner, Outer, Right, and Left. For perspective, the following two tables will be used.
What is the difference between an Inner Join and an Outer Join?
By definition an Inner Join (also just called a Join, inner is assumed) connects the rows from both tables when there is a match between the columns compared.
An Inner Join or simply Join matches when there is a matching condition as above between the type_id in the Items table and the id in the Types table. Notice that we did not get the 'Brobee' record. There was no matching id to the specified type.
What is we want to get all the records regarless if there is a match in our condition. In that case, use an Outer Join. Outer is slightly different in that it is comprised of a Left or Right Join.
What is a Right Join and Left Join?
The above left joins records regardless if there is a match for our type_id. All the records were retrieved regardless of a match on our left side table. Conversely, a Right Join will match only the right side of our condition.
In summation:
There are four major types of joins: Inner, Outer, Right, and Left. Right and Left are part of an outer join. Inner retrieves all matches from the specified join condition(s). Right retrieves from the right table regardless of a match for the left and Left, okay well you have it now.
Yet another of those common technical interview questions that every interviewer wants to know, every candidate gets wrong and everyone looks up anyway when using them. There are four types of joins that this article focuses on Inner, Outer, Right, and Left. For perspective, the following two tables will be used.
|
| |||||||||||||||||||||||||||||||||||||
What is the difference between an Inner Join and an Outer Join?
By definition an Inner Join (also just called a Join, inner is assumed) connects the rows from both tables when there is a match between the columns compared.
SELECT tblTypes.TYPE, tblItems.NAME FROM tblItems JOIN tblTypes ON tblItems.tblTYPE_ID = tblTypes.ID
| TYPE | NAME |
|---|---|
| Animal | Ocelot |
| Vegetable | Cardoon |
| Animal | Banteng |
| Animal | Guenon |
| Mineral | Vomicine |
What is we want to get all the records regarless if there is a match in our condition. In that case, use an Outer Join. Outer is slightly different in that it is comprised of a Left or Right Join.
What is a Right Join and Left Join?
SELECT tblTypes.TYPE, tblItems.NAME FROM tblItems LEFT JOIN tblTypes ON tblItems.tblTYPE_ID = tblTypes.ID
| TYPE | NAME |
|---|---|
| Animal | Ocelot |
| Vegetable | Cardoon |
| Animal | Banteng |
| Animal | Guenon |
| Mineral | Vomicine |
| NULL | Brobee |
SELECT tblTypes.TYPE, tblItems.NAME FROM tblItems RIGHT JOIN tblTypes ON tblItems.tblTYPE_ID = tblTypes.ID
| TYPE | NAME |
|---|---|
| Animal | Ocelot |
| Vegetable | Cardoon |
| Animal | Banteng |
| Animal | Guenon |
| Mineral | Vomicine |
| TBD | NULL |
There are four major types of joins: Inner, Outer, Right, and Left. Right and Left are part of an outer join. Inner retrieves all matches from the specified join condition(s). Right retrieves from the right table regardless of a match for the left and Left, okay well you have it now.
05 April 2011
SQL Having Clause
When would one use the HAVING keyword in SQL?
It seems to me that the HAVING keyword in SQL is a common question asked in technical interviews. No one uses it though if you're writing code, chances are you'll pull all the data out and get what you need through the code. Looking at the HAVING keyword you may realize it can be quite useful to let SQL do all the heavy lifting and skip the WHERE.
First, lets establish a basic table for our example...
So there we have a table. First lets get the quantities of everything.
What can HAVING do for us? It can give a way to search the GROUP BY data easily. So, lets just look for quantities greater than 12.
How is HAVING different from WHERE?
The HAVING keyword is followed by some aggregate function SUM, AVG, MIN, MAX. You cannot do the aggregate functions with the WHERE clause and this is a primary difference. When you're doing a WHERE, you're looking at a row by row comparison versus HAVING which is working with grouped data.
It seems to me that the HAVING keyword in SQL is a common question asked in technical interviews. No one uses it though if you're writing code, chances are you'll pull all the data out and get what you need through the code. Looking at the HAVING keyword you may realize it can be quite useful to let SQL do all the heavy lifting and skip the WHERE.
First, lets establish a basic table for our example...
| ID | TYPE | QUANTITY | DESCRIPTION |
|---|---|---|---|
| 1 | Animal | 8 | Ocelot |
| 2 | Vegetable | 4 | Cardoon |
| 3 | Animal | 4 | Banteng |
| 4 | Animal | 1 | Guenon |
| 5 | Vegetable | 8 | Fluted Pumpkin |
SELECT type, SUM(quantity) as total from table GROUP BY type
We'd end up with:
| type | total |
|---|---|
| Animal | 13 |
| Vegetable | 12 |
SELECT type, SUM(quantity) as total from table GROUP BY type HAVING SUM(quantity) > 12
| type | total |
|---|---|
| Animal | 13 |
How is HAVING different from WHERE?
The HAVING keyword is followed by some aggregate function SUM, AVG, MIN, MAX. You cannot do the aggregate functions with the WHERE clause and this is a primary difference. When you're doing a WHERE, you're looking at a row by row comparison versus HAVING which is working with grouped data.
Subscribe to:
Posts (Atom)


