Jump to content

115 posts in this topic

Posted

[quote name='MrA' timestamp='1349906181' post='595239039']
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.
[/quote]
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 this post


Link to post
Share on other sites

Posted

[quote name='Aethec' timestamp='1350068800' post='595243329']
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.
[/quote]

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 this post


Link to post
Share on other sites

Posted

[quote name='neufuse' timestamp='1350072287' post='595243507']
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...
[/quote]

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 this post


Link to post
Share on other sites

Posted

[quote name='MrA' timestamp='1350109996' post='595244235']
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.
[/quote]
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 this post


Link to post
Share on other sites

Posted

[quote name='MrA' post='595244235']Well, that's partially why I mentioned that the question is poorly defined.[/quote]Then define properly the request. "Character" in Java means "Unicode scalar value" {\u0000

Share this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

[quote name='Coolicer' timestamp='1348957679' post='595215811']
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: [code]A, B = B, A [/code]
But still, internally I'm sure a temporary variable(s!) is being used.


EDIT: Forgot to add my solution.
[CODE]
void FizzBuzz()
{
for (int num = 1; num < 101; num ++)
{
bool multiple = false;
if (num % 3 == 0)
{
cout << "Fizz";
multiple = true;
}
if (num % 5 == 0)
{
cout << "Buzz";
multiple = true;
}
if (!multiple)
cout << num;
cout << 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).
[/quote]

Why not use an else clause rather than a variable?

[code]void FizzBuzz()
{
for (int num = 1; num < 101; num ++)
{
if (num % 3 == 0)
{
cout << "Fizz";
}
else if (num % 5 == 0)
{
cout << "Buzz";
}
else
cout << num;
cout << endl;
}
}[/code]

Share this post


Link to post
Share on other sites

Posted

[quote name='Hardcore Til I Die' timestamp='1350215651' post='595246289']
Why not use an else clause rather than a variable?[/quote]

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 this post


Link to post
Share on other sites

Posted

[quote name='Hardcore Til I Die' timestamp='1350215651' post='595246289']


Why not use an else clause rather than a variable?

[code]void FizzBuzz()
{
for (int num = 1; num < 101; num ++)
{
if (num % 3 == 0)
{
cout << "Fizz";
}
else if (num % 5 == 0)
{
cout << "Buzz";
}
else
cout << num;
cout << endl;
}
}[/code]
[/quote]You failed!

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

Share this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

 

  • 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 this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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;
using System.Threading.Tasks;

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);
                }
            }
            Console.ReadLine();
        }
    }
}

Something wrong with the code? :)

Share this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

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 this post


Link to post
Share on other sites

Posted

> Your favorite interview questions

 

"When can you start?"

 

</thread>

 

3 people like this

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

2 people like this

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

"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 this post


Link to post
Share on other sites

Posted

Guys, this thread is from 2012.

Share this post


Link to post
Share on other sites

Posted

Guys, this thread is from 2012.

good threads don't die :laugh:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.