Interview Question


Country: United States




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

1. Generate all possible pairs and compare their sum with some max_value variable. Keep updating the max_value variable till it satisfies our criteria of sum < given integer but max than max_value. Time complexity O(n^2)

2. Sort the list. Traverse list from both end and compare the sum with given integers. Increase left side if you sum becomes less than given integer else decrease right side.

arr = [2,1,3,2,4,1,10,9,7,7]
sum = 6
arr = sorted(arr)
max_val = 0
max_val_pair = [0,0]
i, j = 0, len(arr)-1

while i<j:
	if arr[i]+arr[j] > sum :
		j-=1
	else :
		if arr[i]+arr[j] > max_val and arr[i]+arr[j]<sum :
			max_val = arr[i] + arr[j]
                        max_val_pair[0], max_val_pair[1] = arr[i], arr[j]
		i+=1
print(max_val_pair)

- gsharma34065 July 13, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question is to find out a pair of integers in the integer array, which meets the special condition, but not the sum of a pair of integers. So, you need to have a pair of indexes to keep track when the max_val is updated. Also, the second condition shall be less or equal instead of less.

- fz July 13, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why the answer is not { 120, 125 } ?

{{
func main() {

arr := []int{90, 85, 75, 60, 120, 150, 125}
integer := 250
heap := generateHeapOfPairs(integer, arr)
fmt.Println("All pairs", heap.ToArray())
if !heap.IsEmpty() {
fmt.Println("Largest Pair", heap.RootElement().ToString())
}

}

func generateHeapOfPairs(limit int, arr []int) minMaxHeap.Heap {
length := len(arr)
pairs := make([]minMaxHeap.Comparable, 0, length*length)

for i := 0; i < length; i++ {

for j := 0; j < length; j++ {
p := Pair{arr[i], arr[j]}
if j > i && p.Sum() < limit {
pairs = append(pairs, p)
}
}
}

return minMaxHeap.NewMaxHeap(pairs)
}

}}

- Felipe Pina July 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution 2 - Why the answer is not { 120, 125 } ?

func main() {

	arr := []int{90, 85, 75, 60, 120, 150, 125}
	integer := 250
	

	maxPair := getLargestPair(integer, arr)
	fmt.Println("getLargestPair", maxPair)
}

func getLargestPair(limit int, arr []int) Pair {
	length := len(arr)
	max := Pair{0, 0}
	for i := 0; i < length; i++ {

		for j := 0; j < length; j++ {
			p := Pair{arr[i], arr[j]}
			if j > i && p.Sum() < limit && p.Sum() > max.Sum() {
				max = p
			}
		}
	}

	return max
}

- Felipe Pina July 15, 2019 | 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