mdfst13
  • Member for 6 years, 9 months
  • Last seen this week
Check if more engineers are happy than not happy
Accepted answer
35 votes

Consider private boolean isMajorityHappy(List<Engineer> engineers) { int netHappiness = 0; for (Engineer engineer : engineers) { if (engineer.isHappy()) { ...

View answer
C program that reverses a string
18 votes

char input[500], output[500] = {}, buffer[2] = {' ', '\0'}; This is not a good way to allocate memory in C. What this means in C is that the memory is only guaranteed unique during the scope of the ...

View answer
Determining if three numbers are consecutive
17 votes

The problem that I have with writing something like the original code is that it is complicated to be sure that it does the right thing in every situation. You've taken three values and perform ...

View answer
Blackjack game interview task
Accepted answer
16 votes

Avoid repetitive code public enum CARD { S2(2),S3(3),S4(4),S5(5),S6(6),S7(7),S8(8),S9(9),S10(10),SJ(10),SQ(10),SK(10),SA(11), D2(2),D3(3),D4(4),D5(5),D6(6),D7(7),D8(8),D9(...

View answer
Tetris in C, in 200 lines
Accepted answer
16 votes

Minimize math operations if (((double)after.tv_sec*1000000 + (double)after.tv_usec)-((double)before.tv_sec*1000000 + (double)before.tv_usec) > timer){ //time difference in microsec ...

View answer
Ncurses Tic Tac Toe with simplistic AI
Accepted answer
13 votes

Avoid numbered variables char space_1 = ' '; char space_2 = ' '; char space_3 = ' '; char space_4 = ' '; char space_5 = ' '; char space_6 = ' '; char space_7 = ' '; char space_8 = ' '; char space_9 = ...

View answer
Simple Blackjack game in console
13 votes

Avoid singletons private static Blackjack game; and later public Blackjack() { initializeGame(); dealCards(); takeTurns(); declareWinner(); ...

View answer
Project Euler #5
Accepted answer
13 votes

if (num % multiple != 0) { isMultiple = false; } I got a five times speed improvement simply by adding a break; to this. if (num % multiple != 0)...

View answer
Determining if the kangaroos will land in the same position
12 votes

Put business logic in functions The first thing that I would do is create a function. Then main becomes int main(){ int x1, v1, x2, v2; std::cin >> x1 >> v1 >> x2 >>...

View answer
Capitals and countries game
Accepted answer
12 votes

Match what you say to what you are actually doing for (int d = 1; d<4; d++) { You want to loop three times. Why does it say 4? Either for (int d = 1; d <= 3; d++) { or more ...

View answer
A custom String Builder implementation
12 votes

public class NoksStringBuilder { When told that something implements StringBuilder, I would expect it to implement the same interfaces. public class NoksStringBuilder implements CharSequence, ...

View answer
How to rewrite if.. else statements in a more elegant way
11 votes

switch I would find a switch cleaner. private static Item createItemType(final Integer type) { if (type != null) { switch (type) { case 0: return Item.PLACE; ...

View answer
Phone number duplication detector
Accepted answer
11 votes

Chomp the new line scan.nextLine(); // But why? Nothing to do with performance, but Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()? explains this. ...

View answer
Yet another lexical analyser
Accepted answer
11 votes

Nitpick private boolean exausthed = false; Pedantic, but this should be spelled exhausted. Class vs. Object private Set<Character> blankChars = new HashSet<Character>(); And ...

View answer
Given three numbers, find the second greatest of them
Accepted answer
11 votes

I would expect main to look like int main(void) { int a, b, c; scanf("%d %d %d", &a, &b, &c); printf("%d\n", find_second(a, b, ...

View answer
Sort an array of 0s, 1s and 2s in Java
Accepted answer
10 votes

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.g. private static void ...

View answer
HackerRank Sherlock and Squares: count perfect squares in an interval
10 votes

using namespace std; Why is “using namespace std;” considered bad practice? Personally, I find code more readable if it says what namespace is being used. E.g. std::sqrt. unsigned test_cases, ...

View answer
SortedStack implementation in Java
10 votes

Not a Stack A Stack is a data structure where items are removed Last-In-First-Out (LIFO). This data structure does not do that. Therefore, it is not a Stack. Calling it a SortedStack is confusing ...

View answer
Playing Card & Deck Class in Java
10 votes

Naming private static final String BACK_IMAGE_DIR = ("image/back_image.png"); You call this BACK_IMAGE_DIR which suggests that it is a directory, but it's actually a file path. Just BACK_IMAGE ...

View answer
Software ECC embedded with a parallel NAND Flash interface
Accepted answer
9 votes

Shift the constants not the variable if (column_sum & 1) { even_column_code ^= 7; odd_column_code ^= 0;} column_sum >>= 1; if (column_sum & 1) { even_column_code ^= 6; ...

View answer
Sudoku verifier in Java
9 votes

Boolean duplicate = false; Set<Integer> set= new HashSet<>(); for(int i : array){ if(set.contains(i)){ duplicate = true; } else { set.add(i); ...

View answer
Project Euler # 7 in C++
Accepted answer
9 votes

I would like to know if there is anything else I can do to check even fewer numbers for an even more efficient algorithm. The general recommendation is to use a That said, there are some other ...

View answer
K&R C book, Exercise 1-21: Replace tabs with spaces
Accepted answer
8 votes

Constants #define TAB_SIZE 4 This is the old way of doing things. Modernly C uses the same syntax as C++: const int TAB_SIZE = 4; While both will still function, this has the added benefit of ...

View answer
Sort a binary file without loading it into memory or using a temporary file
Accepted answer
8 votes

Don't Repeat Yourself (DRY) fseek(dat, i*sajz, SEEK_SET); fread(&tx, sajz, 1, dat); rewind(dat); You do this three times. The general rule is first time ...

View answer
Project Euler 3: Largest prime factor
8 votes

//if i is a factor, check if its prime It may be pedantic, but "its" should have an apostrophe in it if it is used as a contraction for "it is". // if i is a factor, check if it's prime ...

View answer
Checking for Roman numeral validity
Accepted answer
8 votes

Bugs This function will accept invalid strings as valid Roman numerals, e.g. IXC, XCX, IIIII, DD, VL, or A. You always advance by one character. If you're going to do that, you should use a state ...

View answer
Remove duplicate lines without sorting or removing empty lines
Accepted answer
7 votes

Why not just print if /^\s*$/ || ! $SEEN{$_}++ I don't see the point of nesting here. You want to print if the line is empty or if it has not been seen previously. So say that. If you still don't ...

View answer
Solving a sudoku with backtracking
Accepted answer
7 votes

To remove duplication, // check block 1 if (row >= 0 && col >= 0 && row <= 2 && col <= 2){ if(!isBlockValid(n, row, col, board, 0, 2, 0, 2)){ ...

View answer
Return a String containing only alphabets without spaces
Accepted answer
7 votes

char ch; In general, we want to declare as late and at as small a scope as possible. You never use this outside the loop, so it could just be char ch = string.charAt(i); inside the loop. ...

View answer
Checking if two time intervals overlap
Accepted answer
7 votes

Negating the no-overlap case You mention four separate cases where two time intervals overlap. You then reduce it to three by pointing out that one case is really the intersection of two of the ...

View answer
1
2 3 4 5
19