I recently had to do a codility test for a job interview. Now I realise I need to brush up on some basics. They offer "lessons" which I've started going through, but they don't make for very good lessons because they only test you and don't provide examples of good answers. I've done the first 3 tests in the lesson set, and wondered if there were better implementations possible than what I came up with.
Let's discuss lesson 2: https://app.codility.com/programmers/lessons/2-arrays/ (you probably need a free account first)
For CyclicRotation it only cares about correctness, not performance, but is there a clever fast implementation that doesn't involve creating as many new arrays as requested rotations?
Here's my answer for OddOccurrencesInArray (written in Go):
They gave me 100% for correctness and performance ("Detected time complexity: O(N) or O(N*log(N))"). I thought this was the obvious "dumb" way and a bit icky, but couldn't think of anything cleverer. Can you?
Let's discuss lesson 2: https://app.codility.com/programmers/lessons/2-arrays/ (you probably need a free account first)
For CyclicRotation it only cares about correctness, not performance, but is there a clever fast implementation that doesn't involve creating as many new arrays as requested rotations?
Here's my answer for OddOccurrencesInArray (written in Go):
Code:
func Solution(A []int) int {
h := make(map[int]int)
for _, n := range A {
h[n]++
}
for n, c := range h {
if c % 2 != 0 {
return n
}
}
return 0
}
They gave me 100% for correctness and performance ("Detected time complexity: O(N) or O(N*log(N))"). I thought this was the obvious "dumb" way and a bit icky, but couldn't think of anything cleverer. Can you?