Skip to main content
39votes

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
36votes
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.4k
34votes

Checking if a number is power of 2 or not

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

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
28votes

Deck of cards as an interview exercise

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

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
26votes
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. Given ...
Joseph's user avatar
  • 25.3k
26votes
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
25votes
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,508
23votes
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
22votes

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
  • 58k
20votes

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
20votes
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
  • 112k
20votes
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
18votes
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
18votes
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
17votes
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
17votes
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
16votes
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,273
16votes
Accepted

Blackjack game interview task

Avoid repetitive code ...
mdfst13's user avatar
  • 21.9k
16votes
Accepted

Tests for palindromes in C and C++

There are some things to be said about your C version as well, but since you explicitly asked about the C++ version (and also because my C-knowledge is not that great), I will leave those for somebody ...
Ben Steffan's user avatar
16votes

Leetcode: Valid parentheses

This is a follow up of @Henrik Hansen. Instead, of a switch I would use a Dictionary<T, K>. A Dictionary offers two main advantages: an increase readibility ...
aloisdg's user avatar
16votes
Accepted

Converting Roman numerals to integers

Don't use sum as a variable. Especially because you want to use it with your approach! ...
AJNeufeld's user avatar
15votes
Accepted

LeetCode: LRU cache implementation C#

The indentation is inconsistent: the test class has its {} indented the same as the class declaration, but the main class has them indented one level more. It's ...
Peter Taylor's user avatar
15votes

Conway's Game of Life Object oriented implementation in Java

Thanks for sharing your code! OOP doesn't mean to "split up" code into random classes. The ultimate goal of this is to reduce code duplication, improve readability and support reuse as well ...
Timothy Truckle's user avatar
14votes
Accepted

Equation Evaluator

In real life, I would use knowledge of existing libraries to pull out an almost-finished solution. In particular, I think it is an example for Boost.Spirit, which is what I would use for a real ...
JDługosz's user avatar
13votes

Checking if a number is power of 2 or not

It incorrectly returns false when the input is 1. \$2^0 = 1\$ Looping up to number is very ...
CodesInChaos's user avatar
13votes

xml is XML is xml is XML

Naming There are .NET Naming Guidelines which state that methods should be named using PascalCase casing. You haven't done this for ...
Heslacher's user avatar
13votes

Elevator design (interview)

As an interviewer, I would consider this to be a failed attempt. Simply put, it's conceptually wrong. No elevator that I have ever encountered acts as a queue. In a single-elevator system, it should ...
200_success's user avatar

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

close