Interview Question

Entry Level Back-end Engineer Interview

-New York, NY

The Codility Test was about taking an integer and determine how many different arrangements of the number are possible. So if given, 132, arrangements are 132, 123, 231, 213, 312, 321. So the function should return 6.

1

Python solution: import itertools def solution(N): s = list(str(N)) solution = set([ s for s in itertools.permutations(s)]) if len(solution) == 1: print("1") return final_solution = [ s for s in solution if s !='0' ] print(len(final_solution))

Cand on

0

import collections def fact(n): factorial = n for i in range(1, n): factorial *= i return factorial def better_solution(N): s = list(str(N)) string_counts = collections.Counter(s) print(string_counts) permutations = fact(len(s)) # dealing with duplicate string values for string, count in string_counts.items(): if count > 1: permutations //= fact(count) return permutations

NA on

3

#include #include using namespace std; int factorial(int n){ return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n; } // Returns number of occurences of char c in string s int numberOfOccurences(string s, char c){ int occurences = count(s.begin(), s.end(), c); return occurences; } int solution (int N){ string numString = to_string(N); int numOfDigits = numString.length(); int computation = factorial(numOfDigits); // Using ASCII Codes for (int i = 48; i < 58; i++) { char currentChar = (char) i; int occurences = numberOfOccurences(numString,currentChar); if (occurences != 0){ computation = computation / (factorial(occurences)); } } return computation; }

Anonymous on