Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

Probably - and it is highly probably - the sequences are simply integers one after another.
So, given start (1) and end(3) the sequences can be :
( You dont have to select all the elements )

1,1,1,1
2,2,
1,1,2,2
1,2,1,2
1,2,2,1
2,1,1,2
1,3
3,1

.... etc.

- NoOne January 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package main

import "fmt"

// _GetAllSums variants of summ
func _GetAllSums(A, N int) []map[int]int {
	if N == 0 || A == 0 {
		return []map[int]int{{}}
	}
	if A > N {
		return _GetAllSums(N, N)
	}
	if A == 1 {
		return []map[int]int{{1: N}}
	}
	result := _GetAllSums(A-1, N)
	t := N - A
	countA := 1
	for t >= 0 {
		tmp := _GetAllSums(A-1, t)
		for _, v := range tmp {
			v[A] = countA
			result = append(result, v)
		}
		t -= A
		countA++
	}
	return result
}

func Factorial(N int) int64 {
	if N < 0 {
		N = -N
	}
	result := int64(1)
	for i := N; i > 1; i-- {
		result = result * int64(i)
	}
	return result
}

func CountSums(A, N int) int64 {
	r := _GetAllSums(A, N)
	result := int64(0)
	// Calculate result with permutations with repeating elements
	for _, variant := range r {
		length := 0
		denominator := int64(1)
		for _, nums := range variant {
			length += nums
			denominator *= Factorial(nums)
		}
		if length > 0 {
			result += Factorial(length) / denominator
		}
	}
	return result
}

func main() {
	fmt.Println(CountSums(3, 4)) // Output: 7
}

- dmitry.labutin January 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Don't know how we can get this coded but it could be the sum of all numbers between 1-3 repeating/non-repeating to 4.
1111
112
121
13
22
211
31

- Anonymous January 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With python generators

def generate_sequence(n):
    i = 0
    while i < n:
        j = i + 1
        while j < n:
            yield [(i,j), i+j]
            j = j + 1
        i = i + 1

def get_sequence_sum(n, a):
    return (x[0] for x in generate_sequence(n) if  x[1] == a)

print list(get_sequence_sum(10, 7))
print list(get_sequence_sum(12, 10))
print list(get_sequence_sum(8, 9))

- Samir January 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With python generators

def generate_sequence(n):
    i = 0
    while i < n:
        j = i + 1
        while j < n:
            yield [(i,j), i+j]
            j = j + 1
        i = i + 1

def get_sequence_sum(n, a):
    return (x[0] for x in generate_sequence(n) if  x[1] == a)

print list(get_sequence_sum(10, 7))
print list(get_sequence_sum(12, 10))
print list(get_sequence_sum(8, 9))

- Anonymous January 30, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More