Skip to main content
Share Your Experience: Take the 2024 Developer Survey
60 votes
Accepted

Interview coding test: Fizz Buzz

Then add a special case In all likelihood, they were looking to see how flexible and maintainable you could make your code be, and you gave them a showcase of some raw technical knowledge instead. I'...
Mathieu Guindon's user avatar
47 votes
Accepted

Keeping track of the tennis score

Overall You missed the point. If you are supposed to create a class to track the score of game you did it wrong. Most of the logic that should be inside the class has been left inside main. If you ...
Martin York's user avatar
  • 95.2k
40 votes
Accepted

Counting permutations without repetitions for a number or a string

One of the best ways to make a program faster is not to compute things that you don't have to. In this question you are asked to compute the number of permutations. But your implementation goes and ...
Gareth Rees's user avatar
  • 49.6k
39 votes

Deck of cards as an interview exercise

No one but the folks who reviewed your code for the company can say with certainty why you were rejected. It also depends on the position you're applying for. If you're applying for an entry-level ...
Chris G's user avatar
  • 501
37 votes
Accepted

PI Calculator, Interview Challenge

In an interview it usually doesn't matter if you actually solve the problem. What is most important is the way you (try to) solve it. If they don't tell you it should be the possibly fastest solution ...
t3chb0t's user avatar
  • 44.3k
35 votes

Checking if a number is power of 2 or not

There's actually a bit hack for this : ...
bowmore's user avatar
  • 5,369
29 votes

Generate a list of 10,000 numbers in random order

In general First and foremost, you were asked to produce a program that generates a list of 10,000 numbers in random orders. You've added far too much complexity. There's no need for input or output (...
Rob's user avatar
  • 652
28 votes

Deck of cards as an interview exercise

This is mostly how I would solve it (in Python): ...
Zah's user avatar
  • 450
27 votes
Accepted

Checking if a number is power of 2 or not

try-with-resources Since Java 7, you should use try-with-resources on your Scanner for safe ...
h.j.k.'s user avatar
  • 19.2k
26 votes

Interview coding test: Fizz Buzz

Java 8 has good support accross all IDEs but there are still a few differences. For example your code, as-is, doesn't compile with the latest Eclipse version (Mars.2). On the command line with JDK 1.8....
Tunaki's user avatar
  • 9,243
26 votes
Accepted

Detect the missing number in a randomly-sorted array

You don't need to calculate the expected sum in a loop since you can use the formula: \$ S = (a_1 + a_n) * n / 2 \$ Also it makes sense to use LINQ to get the actual sum: ...
Dmitry's user avatar
  • 4,589
26 votes

PI Calculator, Interview Challenge

I'm going to give an alternative approach to tchbot's answer There are places where SOLID prinicpals are required. I don't see this as one of them. Here, we have a simple "Are you capable of ...
Andrew Shepherd's user avatar
26 votes
Accepted

Deck of cards as an interview exercise

While not strictly required, we value usage instructions, nicely-modeled data, automated tests, and thoughtful consideration of architectural decisions and simplicity-vs-completeness trade-offs. ...
Joseph's user avatar
  • 25.1k
25 votes

Interview coding test: Fizz Buzz

I apologize if the tone of this is a bit harsh, but hopefully I can provide some valuable advice. For me, Fizzbuzz is about three things: Is there a loop? Is there a function? Does the code run? ...
brian_o's user avatar
  • 839
25 votes
Accepted

Check if a string has all unique characters

People are very eager to present their own solutions, so let me attempt an actual review of your code: Your names could be clearer. Strings is not very descriptive,...
yuri's user avatar
  • 4,458
23 votes
Accepted

Checking if two strings are anagrams in Python

You can change your if to just be return. You should change your while to an ...
Peilonrayz's user avatar
  • 43.4k
22 votes

Tests for palindromes in C and C++

A special case length < 3 seems like a bug. A string aa is a palindrome, and one could successfully argue that a single-...
vnp's user avatar
  • 56.5k
20 votes

PI Calculator, Interview Challenge

Flavio's answer addresses a part of something that matters greatly for this kind of problem: rounding errors will kill you. If you look at the expression: $$1-\frac13+\frac15-\frac17+...$$ This is ...
Floris's user avatar
  • 397
20 votes
Accepted

Palindrome-testing Java program for an interview

It's enough to loop until word.length() / 2, as this will compare the first half with the second half, so no need to go until the end. As you use ...
janos's user avatar
  • 111k
20 votes
Accepted

Pancake sort interview with Python

flip For starter, you can swap two variables in Python without using a temporary one: ...
301_Moved_Permanently's user avatar
18 votes
Accepted

Find the first non-repeating character in a string: ("DEFD" → E)

It's easier to do it with LINQ which is in this case virtually a one-liner. ...
t3chb0t's user avatar
  • 44.3k
18 votes
Accepted

FizzBuzz Implementation in Java

The point of FizzBuzz is to weed out obviously unqualified candidates, not to over-optimize the solution or to show off. Your code is a fine interview answer. Make sure that your braces all match up....
200_success's user avatar
18 votes
Accepted

Roman to Integer

Packaging and style Why does this function accept self as the first parameter? You never use it anywhere, and it doesn't look like this function should be a ...
200_success's user avatar
17 votes

Keeping track of the tennis score

@Loki Astari is completely right in his points. Apart from that you should consolidate your logic, as currently you have 5 clauses for 3 cases, which are: ...
miscco's user avatar
  • 4,271
17 votes
Accepted

Determine if an array is the reverse of a second array

The first comment, is that it would be preferable to factor this into a dedicated method, like areReversed(int[] array1, int[] array2). It would clarify the ...
Tunaki's user avatar
  • 9,243
17 votes
Accepted

Get all combinations of selecting k elements from an n-sized array

Binomial coefficient \$\binom{n}{k}\$ is growing too fast (with \$n\$) to keep a list of all combinations in memory. I would suggest to use yield return instead. ...
pgs's user avatar
  • 922
17 votes
Accepted

Count the occurrence of each unique word in the file

Efficiency I won't say much about efficiency - because without a clear use-case it will be hard to know whether possible changes would be worth the effort - but my main concern would be fact that you ...
VisualMelon's user avatar
  • 7,431
17 votes
Accepted

Conway's Game of Life Object oriented implementation in Java

It's hard to judge a design when there is no information about what you want from it. If you wanted classes and methods, well you succeeded, but that's about it. But if you, for example, wanted ...
TorbenPutkonen's user avatar
16 votes

Interview coding test: Fizz Buzz

Your solution probably looks too different from how they would have coded it themselves, and so it looks like complete overkill for what they're asking for. Basically you've just told them that you ...
Tom Eberhard's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible