Skip to main content
34 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
26 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.4k
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,548
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
  • 44.5k
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
  • 58.7k
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
20 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
18 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
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
  • 932
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,581
16 votes
Accepted

Blackjack game interview task

Avoid repetitive code ...
mdfst13's user avatar
  • 22.4k
16 votes
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
  • 5,333
16 votes

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
  • 561
16 votes
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
  • 35.3k
15 votes
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
  • 24.5k
15 votes

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
14 votes
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
  • 11.7k
13 votes

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
  • 2,680
13 votes

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
  • 51k
13 votes

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

Tests for palindromes in C and C++

I've seen a lot of answers here over-generalizing. You're dealing with a std::string, so you can just do: ...
David's user avatar
  • 1,058
13 votes
Accepted

String Encoder with lower-case output

There are couple of things that might have been done better... Initilize the dictionary form a static constructor or call the method to initialize the field. The method should not access it internally ...
t3chb0t's user avatar
  • 44.7k
13 votes

C++ std::optional implementation for tech interview

Here is the list of issues I've found in the implementation in no particular order: The operator bool should be explicit, otherwise it's allowed to implicitly ...
ALX23z's user avatar
  • 2,539
12 votes
Accepted

Are strings anagram

I think interviewer will not be convinced with this solution because here it is no test of logic. I'm not sure what you mean by "because here it is no test of logic," but if what you mean is "because ...
Quuxplusone's user avatar
  • 19.7k
12 votes
Accepted

Sort an array of 0s, 1s and 2s in Java

Descriptive names private static void printSortedArr (int[] arr) { I don't like these names. I would rather write out array if that's what's being said. E....
mdfst13's user avatar
  • 22.4k
12 votes
Accepted

Product of all but one number in a sequence

Here are some things that may help you improve your code. Use all required #includes The code uses memcpy, so it should ...
Edward's user avatar
  • 67.2k
11 votes
Accepted

Remove duplicate characters in a string

Clarify the requirement You state 'remove duplicate characters' but then your code checks for upper and lower case matches - is the requirement to check for distinct letters, or distinct characters? ...
Phil H's user avatar
  • 396
11 votes

Find char which appears maximal number of times

According to a modern C# and LINQ you could try to "shock" interviewer with solving in a row: ...
HelloWorld's user avatar
11 votes

Area calculator for shapes as an OOP interview test

I see some things that may help you improve your code. Don't overcomplicate your code The only thing required of the Shape and ...
Edward's user avatar
  • 67.2k

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