Followers 0

# Programmers: Your favorite interview questions

## 115 posts in this topic

Hehe. I guess I wasn't clear. It's a trick question (and arguably, poorly defined), and according to my friend, no-one's been able to answer it. I certainly couldn't answer it, but at least I understand why I can't answer it.

The trick is that the naive algorithm only works with a monospace font. The extreme case, a string composed of 'i's, will hold much more letters than normal text in a certain amount of pixels. So you have to measure the text if you want to do it correctly.

##### Share on other sites

The trick is that the naive algorithm only works with a monospace font. The extreme case, a string composed of 'i's, will hold much more letters than normal text in a certain amount of pixels. So you have to measure the text if you want to do it correctly.

but the way he wrote the question it said nothing about text width

2. In Java, write a function which takes a string and returns a string that is the input if the input is 6 characters or less, and the first 3 characters + "..." if it's greater than 6.

that is pretty straight forward, you are only careing about the number of characters... now if you said that as fit a string of text into a row that is 250 pixels wide with the font arial, and type settings of 12pt and bold, itallic, replace any overlap with an elipsis... that would make more sense... can't be a trick question if it only says take 6 char's replace anything over 6 with the first 3 plus an elipsis...

2 people like this

##### Share on other sites

but the way he wrote the question it said nothing about text width

2. In Java, write a function which takes a string and returns a string that is the input if the input is 6 characters or less, and the first 3 characters + "..." if it's greater than 6.

that is pretty straight forward, you are only careing about the number of characters... now if you said that as fit a string of text into a row that is 250 pixels wide with the font arial, and type settings of 12pt and bold, itallic, replace any overlap with an elipsis... that would make more sense... can't be a trick question if it only says take 6 char's replace anything over 6 with the first 3 plus an elipsis...

Well, that's partially why I mentioned that the question is poorly defined. But the main issue is that Java strings are UTF-16, NOT UCS-2. So if you have a string where the first code point is a surrogate pair, then the naive algorithm will produce an invalid string. So what you want is code points. Of course, that's not the entire story. What do you do about control characters? You probably want to interpret those ones you can, like BOM. But what about accents and other "modifiers". Those aren't entire characters, so you probably want to treat "e" and circumflex as a single character. And of course, if the reason why you doing this is to display the string in a restricted space, then the whole "6 characters" is moot since you now need to take into account font, size, weight, and any number of other rendering factors. So overall, not an easy question.

##### Share on other sites

Well, that's partially why I mentioned that the question is poorly defined. But the main issue is that Java strings are UTF-16, NOT UCS-2. So if you have a string where the first code point is a surrogate pair, then the naive algorithm will produce an invalid string. So what you want is code points. Of course, that's not the entire story. What do you do about control characters? You probably want to interpret those ones you can, like BOM. But what about accents and other "modifiers". Those aren't entire characters, so you probably want to treat "e" and circumflex as a single character. And of course, if the reason why you doing this is to display the string in a restricted space, then the whole "6 characters" is moot since you now need to take into account font, size, weight, and any number of other rendering factors. So overall, not an easy question.

I don't know about all accents, but in UTF-16 the circumflex and other common accents are not separated from the character. (IIRC, windows-1257 does that, which is useful to remove accents from a string by converting it back into ASCII)

But your question was worded in a way that makes "s.length <= 6 ? s : s.substring(0,3)" an acceptable answer.

##### Share on other sites
Well, that's partially why I mentioned that the question is poorly defined.
Then define properly the request. "Character" in Java means "Unicode scalar value" {\u0000 ? \u10FFFF}.
And of course, if the reason why you doing (?)
It is irrelevant because it was not stated in the request.

##### Share on other sites

I have a really hard time believing any "programmer" could be eliminated by the fizzbuzz question (I wish I had easy questions like that for my interviews :/)

Personally I think most "interviews" for developers are ridiculous and are usually aimed at making the person fail (only tool you have are pencil and paper and the questions are most of the time extremely obscure and could be answered with more time/better tools)

I know most people I work with that are "good" at interviews are usually just that, most of their code usually look like crap.

##### Share on other sites

That's the whole pitfall of FizzBuzz (unless you do duplicate checks, which is usually not wanted)

Could you give the solution? Or is the solution language specific, like Python assigment:

`A, B = B, A `

But still, internally I'm sure a temporary variable(s!) is being used.

EDIT: Forgot to add my solution.

```void FizzBuzz()
{
for (int num = 1; num  &lt; 101; num ++)
{
bool multiple = false;
if (num % 3 == 0)
{
cout &lt;&lt; "Fizz";
multiple = true;
}
if (num % 5 == 0)
{
cout &lt;&lt; "Buzz";
multiple = true;
}
if (!multiple)
cout &lt;&lt; num;
cout &lt;&lt; endl;
}
}
[/CODE]

I'm still not sure if it's better to declare a variable outside of the loop, but I'm fairly sure it will be optimized by the compiler.
And my solution is fairly readable (I hope).

Why not use an else clause rather than a variable?
[code]void FizzBuzz()
{
for (int num = 1; num  &lt; 101; num ++)
{
if (num % 3 == 0)
{
cout &lt;&lt; "Fizz";
}
else if (num % 5 == 0)
{
cout &lt;&lt; "Buzz";
}
else
cout &lt;&lt; num;
cout &lt;&lt; endl;
}
}```

##### Share on other sites

Why not use an else clause rather than a variable?

Because in the case of %3 && %5 you need to print 'FizzBuzz'. Now you don't and have failed the assignment (into it's pitfall).

That's why I have a boolean variable that keeps track of whether it printed "Fizz" OR "Buzz". I thought about doing

it differently, something with scopes but that wouldn't do much and my solution is fairly readable and simple.

##### Share on other sites

Why not use an else clause rather than a variable?

```void FizzBuzz()
{
for (int num = 1; num  &lt; 101; num ++)
{
if (num % 3 == 0)
{
cout &lt;&lt; "Fizz";
}
else if (num % 5 == 0)
{
cout &lt;&lt; "Buzz";
}
else
cout &lt;&lt; num;
cout &lt;&lt; endl;
}
}```

You failed!

You're missing the FizzBuzz state (if dividable by both 3 and 5 aka divisible by 15)

##### Share on other sites

I've been asked that question. As soon as you give the solution using modulus, they ask you to do it again without using modulus/multiplication/division.

I assumed by that you meant no arithmetic operations (including bitwise) whatsoever and was like hmm that's impossible. But, I see it was only the particular types.

EDIT: well other than just comparisons over and over...  :laugh:

##### Share on other sites

• Given two integer variables, A and B, how could you swap their values without using a third variable? (This is my favourite. It wasn't actually given to me, but to my housemate at the time. He didn't get it, but the interviewer gave it to him to do on the train home, he figured it out on the train and emailed the answer to the interviewer, subsequently got the job :p)

I think this is a poor question because the only solution is not really an solution since it contains defects that break the intended purpose. Clever tricks with potentially subtle bugs aren't really clever tricks at all. Personally, I would have just given an inline asm solution that only used registers and pointed out that the only memory accesses would be the fetch/storeback of A&B and that no temporary exists in memory. It's not clever and the compiler will do it automatically anyway, but it works in all cases.

EDIT: There's also an XOR solution for this, but it has caveats in practice also.

##### Share on other sites

I rely on their ability to solve logical problems using their own made up syntax. I've only had to hire once though so my experience in it is limited. My train of thought was that I prefer that they can solve complex problems instead of code complex solutions.

FizzBuzz is great and all, but I prefer someone who can learn it quickly as opposed to someone that can (re)code it efficiently; let's be honest, most of the people who pass a fizzbuzz test have already done or practiced a fizzbuzz test. I have (and for merits of ability it's in my interest to inform you all I passed it, but of course).

In the end it worked out great. They were a stronger programmer than I am, which isn't that hard to do lol.

2 people like this

##### Share on other sites

FizzBuzz is a game, not an interview question.  I am with astropheed, look more towards their logical ability than silly games.

##### Share on other sites

Hello,

I actually read you guys talk about FizzBuzz and I think Im missing something:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fizzbuzz
{
class Program
{
static void Main(string[] args)
{
for (int i=1;i<101;i++)
{
if   ((i % 3 == 0) && (i %5==0))
{
Console.WriteLine("FizzBuzz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else if (i%3==0)
{
Console.WriteLine("Fizz");
}
else
{
Console.WriteLine(i);
}
}
}
}
}

```
Something wrong with the code? :)

##### Share on other sites

Hello,

I actually read you guys talk about FizzBuzz and I think Im missing something:

Something wrong with the code? :)

Your code should work... but you might want to think about efficiency. If the number is divisible by 3, but not 5, you end up checking if the number is divisible by 3 twice and 5 twice.

But efficiency can be misleading. Should we optimize for (code) space efficiency, memory efficiency, or time efficiency... and how much longer should we assume a multiply/divide/modulus takes than an add?

##### Share on other sites

Your code should work... but you might want to think about efficiency. If the number is divisible by 3, but not 5, you end up checking if the number is divisible by 3 twice and 5 twice.

But efficiency can be misleading. Should we optimize for (code) space efficiency, memory efficiency, or time efficiency... and how much longer should we assume a multiply/divide/modulus takes than an add?

No point in wasting time or effort on simple optimizations that the compiler is going to do for you anyway: it just obfuscates the high level code for no benefits in practice. Same with throwing away assignments to temporary variables.

##### Share on other sites

No point in wasting time or effort on simple optimizations that the compiler is going to do for you anyway: it just obfuscates the high level code for no benefits in practice. Same with throwing away assignments to temporary variables.

I absolutely agree. However, your interviewer is going to have some questions for you after you finish writing the code. It's good to have some answers about efficiency, and other possible ways to write it.

It's a shame this topic got so hopeless devoted to beating the dead FizzBuzz horse. There are much more interesting questions that are more likely to be asked. Other than Hulu, who asked me to code FizzBuzz, every other interview I have been on have asked questions about linked lists... and sometimes about doubly linked lists.

##### Share on other sites

I absolutely agree. However, your interviewer is going to have some questions for you after you finish writing the code. It's good to have some answers about efficiency, and other possible ways to write it.

True that, any interviewer worth their salt will try to explore the problem and see your logical processes. The thing is, I see suggestions to do those sort of thing in real code in spots where it is obviously not any sort of bottleneck all of the time. In practice, I think it is focusing on the wrong thing in real world optimization.

It's a shame this topic got so hopeless devoted to beating the dead FizzBuzz horse. There are much more interesting questions that are more likely to be asked. Other than Hulu, who asked me to code FizzBuzz, every other interview I have been on have asked questions about linked lists... and sometimes about doubly linked lists.

I've heard google asks some difficult questions (none of that FizzBuzz). Another question I've heard is the algorithm for palindromes, and then to step it up to more difficulty, finding the sub palindromes (including shifted palindromes, e.g. abbattttttttttt, where 'abba' and 'bb' are matches).

##### Share on other sites

"When can you start?"

3 people like this

##### Share on other sites

I ask any programmer if they like their source open or closed.

##### Share on other sites

True that, any interviewer worth their salt will try to explore the problem and see your logical processes. The thing is, I see suggestions to do those sort of thing in real code in spots where it is obviously not any sort of bottleneck all of the time. In practice, I think it is focusing on the wrong thing in real world optimization.

I've heard google asks some difficult questions (none of that FizzBuzz). Another question I've heard is the algorithm for palindromes, and then to step it up to more difficulty, finding the sub palindromes (including shifted palindromes, e.g. abbattttttttttt, where 'abba' and 'bb' are matches).

I've had all of those logical questions (fizzbuzz, palindromes, you name it) on tests and exams for Java, in my 1st year of college (1st semester, really); it really pushes the logical thinking forward so when a student starts learning higher maths, physics and programming it can use the rational thinking to think out of the box.

by the way: i'm not a programmer :)

the problem solving aspect is one of the most valuable things a programmer must have (not only just the programmer, but since this is the topic...), not to mention code knowledge and the ability to learn new stuff. I've had questionable interviews made by psychologists that didn't know jack about the job i was being interviewed (and therefor they used questions that anybody could find on the interwebs, not really doing a good job filtering the interviewer really) or being asked incredible dumb and trick questions that made me want to question why should i want to join an enterprise that can't even do a proper interview; when i answered all those questions i realized that all they wanted is someone that was good but for cheap. :/

so, answering OP (this thread is kind of oldie...2012, eh?): my favorite question i ever had in a interview was:

- HR woman: "well lets see... i think for starting the paycheck will be 1400?..."

- Praetor: /does an incredible stunned face since i don't even get 1000? (tough days we are getting in my country)... 1400? for starting is a very welcome jump!

- HR woman: "oh... you seem really upset; i think we can push to 1500? + bonus, is that OK for you?"

- Praetor: / eyes pupils change to euro signs. :woot:

2 people like this

##### Share on other sites

I've had all of those logical questions (fizzbuzz, palindromes, you name it) on tests and exams for Java, in my 1st year of college (1st semester, really); it really pushes the logical thinking forward so when a student starts learning higher maths, physics and programming it can use the rational thinking to think out of the box.

by the way: i'm not a programmer :)

the problem solving aspect is one of the most valuable things a programmer must have (not only just the programmer, but since this is the topic...), not to mention code knowledge and the ability to learn new stuff. I've had questionable interviews made by psychologists that didn't know jack about the job i was being interviewed (and therefor they used questions that anybody could find on the interwebs, not really doing a good job filtering the interviewer really) or being asked incredible dumb and trick questions that made me want to question why should i want to join an enterprise that can't even do a proper interview; when i answered all those questions i realized that all they wanted is someone that was good but for cheap. :/

so, answering OP (this thread is kind of oldie...2012, eh?): my favorite question i ever had in a interview was:

- HR woman: "well lets see... i think for starting the paycheck will be 1400?..."

- Praetor: /does an incredible stunned face since i don't even get 1000? (tough days we are getting in my country)... 1400? for starting is a very welcome jump!

- HR woman: "oh... you seem really upset; i think we can push to 1500? + bonus, is that OK for you?"

- Praetor: / eyes pupils change to euro signs. :woot:

I think you are absolutely right: many of these are just simple 1st year assignment type things. And, really, they are just used to weed out applicants who can't even do that much. At the same time when you start to step up the difficult and have them work through successively more difficult versions of a similar problem, you get a feel for how the applicant approaches problem solving. That's how I'd conduct an interview myself. For example, asking the interviewee how they'd handle arbitrary sized swaps in the case of the integer swapping, asking what the potential problems are; or in the case of the palindromes asking for a better complexity solution than what is probably going to be offered. Just seeing if they can reason through things like that.

You know, now that you pointed out the thread is from 2012, I'm not even sure HOW I managed to get to it. I could have sworn I clicked on the updated threads or spy and saw it there. I rather do like your answers though. I think they fit the bill better for favorites.

##### Share on other sites

"what is dependency injection, and why would you use it?"

oh man, have I heard some hilariously incorrect answers to that one during interviews.

##### Share on other sites

Guys, this thread is from 2012.

##### Share on other sites

Guys, this thread is from 2012.

## Create an account

Register a new account