39 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 ...
user avatar
  • 49.2k
32 votes
Accepted

Analyzing Minesweeper Probabilities

GroupValues GroupValues doesn't seem to serve much of a purpose beyond what you get from Map; it adds no functionality beyond ...
user avatar
31 votes

Solution for kth row of Pascal's triangle for a job interview

For one thing, the inconsistency with whitespace use and curly brace placing may demonstrate a lack of attention to detail. Before you attack the actual problem, make sure your code is written ...
user avatar
  • 34.7k
28 votes
Accepted

Solution for kth row of Pascal's triangle for a job interview

There are at least two optimizations you could make. First of all, you are performing r multiplications and r divisions for ...
user avatar
  • 1,188
26 votes

Solution for kth row of Pascal's triangle for a job interview

Types are very important in C++. You are being sloppy with your types. long long sol; // big int object. While you are putting it into an object that only ...
user avatar
  • 91.7k
21 votes
Accepted

Count number of ways to paint a fence with N posts using K colors

1. Code review The question says, "Write an algorithm that counts the number of ways" but the program does not only do this: it also builds a list of the combinations. This means that the program is ...
user avatar
  • 49.2k
20 votes
Accepted

Brute Force Algorithm in C

I'm going to skip reviewing the OP's code, which has already been reviewed quite nicely by others. Instead I'm just going to show a much faster version and explain it. What is the algorithm? ...
user avatar
  • 28.4k
19 votes

Solution for kth row of Pascal's triangle for a job interview

I am kind of curious why you separated the Int variable out of the for loop? ...
user avatar
  • 28.5k
19 votes

Solution for kth row of Pascal's triangle for a job interview

As an interviewer, I'd see a big red flag in the implementation. Calculating each coefficient from scratch results in a lot of unnecessary recalculations. Use recurrence: you already know \$\binom{n}{...
user avatar
  • 53.4k
16 votes
Accepted

Generating Cartesian product of strings in R

Your approach is an example of good R code. However, there is a base function that allows creating a cartesian product of strings, interaction. This function ...
user avatar
16 votes

Brute force password cracker

Expectation Setting Your algorithm is an incrementing index, which you then convert in to the radix of your charset. Your charset is what, 95 characters? So, there are the following possible ...
user avatar
  • 96.7k
16 votes

Pairwise combinations of an array in Javascript

Pairing combinations like this is a very common thing to do in any language. There's a very 'idiomatic' way to do this type of operation: ...
user avatar
  • 96.7k
16 votes
Accepted

Calculate all possible distributions of n objects to k containers

The structure of the code makes it difficult to test. Suppose you had two different implementations, and wanted to test that they returned the same results? The code doesn't lend itself well to this ...
user avatar
  • 16.1k
16 votes
Accepted

Printing all possible sums of two n-sided dice rolls

As @BenC mostly put a code dump, I'll explain ways that your code could have improved. reverseIt Programming this is just wrong in Python. This is as there are ...
user avatar
  • 41.7k
16 votes
Accepted

List all possible permutations from a python dictionary of lists

The tool you need here is itertools.product: ...
user avatar
  • 49.2k
16 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. ...
user avatar
  • 912
15 votes

Advanced and Detailed Minesweeper Probabilities

Focusing on optimizations: in GroupValues: ...
user avatar
  • 22.2k
15 votes
Accepted

Approximate (250 over 100) permutation best fitting certain criteria

1. Analysis In the general case, the problem of finding the sample of words with the "most even" distribution of letters is NP-hard. Here I'm considering a general instance of this problem to be: ...
user avatar
  • 49.2k
15 votes
Accepted

Generate a dictionary of passwords

Please take a look at itertools.product. It will simplify your code greatly. ...
user avatar
15 votes

Determine if one string is a permutation of the another

I would say yes, use an array. Arrays afford very good memory locality, which is good for cache performance. They essentially are the most basic and efficient hash map if the key is the index. You ...
user avatar
  • 5,721
14 votes
Accepted

Pairwise combinations of an array in Javascript

Your solution works fine, and it's fairly efficient. You're using loops and mutation, which are speedy, but also not terribly readable. This algorithm can be expressed more functionally in terms of ...
user avatar
  • 3,172
14 votes
Accepted

Minimising the triangle

Style In Java it is common practice to have class names to use PascalCase (a.k.a. UpperCamelCase). Also the name spaceship doesn't make sense to me and is ...
user avatar
  • 16.4k
14 votes

Counting permutations without repetitions for a number or a string

You're looking for set(). The reason for your program being slow is: you're calculating the permutation once: ...
user avatar
14 votes
Accepted

Find the total number of ways W, in which a sum S can be reached in N throws of a dice

1. Review The sum \$s=0\$ can be reached in \$n=0\$ throws in exactly one way, but: ...
user avatar
  • 49.2k
14 votes
Accepted

Determine if one string is a permutation of the another

Comparing two maps is linear, so that's not a big problem. I don't think you've made particularly good use of maps though. As I see it, you have two choices. You could use an ...
user avatar
  • 33.2k
13 votes

Analyzing Minesweeper Probabilities

One of the first rule I try to always follow is to be consistent. You're not always following the same pattern in your code for brackets. ...
user avatar
  • 6,661
13 votes
Accepted

Generate all possible combinations of letters in a word

I'm going to throw my hat in the ring with the iterative version of Heap's method of generating permutations. It took me a while to get right, as my reference source has a typo in the algorithm -_-. <...
user avatar
  • 16.1k
13 votes
Accepted

A beautiful and easy way of permutation with recursion

The simpler code would look something like this: ...
user avatar
  • 33.2k
12 votes

Get distinct combinations of numbers

Let's talk algorithms, and complexity..... and what the actual problem is that you are trying to solve. Background Three concepts: distinct, permutation, and combination Distinct Distinct values ...
user avatar
  • 96.7k
12 votes
Accepted

Advanced and Detailed Minesweeper Probabilities

@Pimgd were definitely on to something big in the last part of the answer, but not on to it enough. Simply changing Solution<T> to ...
user avatar

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