Skip to content

Instantly share code, notes, and snippets.

@shirlymanor
Created March 29, 2017 21:35
Show Gist options
  • Save shirlymanor/9276684fdc78f49dd2203376951afec8 to your computer and use it in GitHub Desktop.
Save shirlymanor/9276684fdc78f49dd2203376951afec8 to your computer and use it in GitHub Desktop.
First question (based on google interview question). Giving a collection of numbers and you will need to find a matching pair that are equal to the number that I'll give you.
import UIKit
/*
Question - I'm giving you a collection of numbers and you will need to find a matching pair that are equal to the number that I'll give you.
So for example the collection of number will be: [1,2,3,9] and the sum that I'm looking for is 8.
An another example will be [1,2,4,4] and the sum is 8
A- so, I'm trying to understand. You are looking for a pair of numbers that add up to 8
S - Yes
A- So in the first example there is no pair of number and in the second example it's 4,4.
S- Correct
A- How do I get this numbers? Can I assume that they are in memory? an array?
S- Yes they are in memory like an array you can also assume that they are ordered.
A- Oh, Interesting! How about repeating elements?
if I'm getting an array of [1,2,4] Can I repeat 4 twice?
S- you can have the same element but not in the same index place.
A- How about this numbers? Are they integers? or floating?
S- you can assume they are all integer
A- negatives?
S- Yes, you can have negative.
A- So, the first simple solution to compare every element in the array. have 2 for loops go over each element in the array. Start with the first and the second element. (brute force) It's not efficient but it's a way to solve it.
S- That would works. It's certainly would be time consuming
A- I'm trying to look if I have 1 in the first place I need to look for a 7 somewhere so I can use binary search.
I can find the sum of two numbers if it bigger then 8 I'll move the higher index and if it's smaller I'll move the low index right. That Will be linear iteration and I end if I find a pair of if they cross. It's better then binary because because the binary will go over every element so it will be Nlog(n) and the linear is just once O(1) ?
S- Can you show me how it's works in a working environment?
A- Show
S- so what coding language do you prefer? Swift :)
*/
//brute force
func isSum(arr:[Int], sum:Int)->Bool
{
for i in 0 ..< arr.count
{
for j in 1 ..< arr.count
{
if(arr[i] + arr[j] == sum){
return true
}
}
}
return false
}
isSum(arr: [2,3,4,4], sum: 8)
// Linear Search
func IsSumlinearSearch( a: [Int], key: Int) -> Bool {
var low = 0
var high = a.count-1
while low < high {
print(a[high])
if (a[low] + a[high] == key ) {
return true
}
if (a[low] + a[high] > key ){
high -= 1
}
else {
low += 1
}
}
return false
}
IsSumlinearSearch(a: [1,1,4,5,6], key: 8)
// What if I'll ask you to return the pair of numbers if it's true?
func IsSumlinearSearchReturnNum( a: [Int], key: Int) -> [Int]? {
var low = 0
var high = a.count-1
while low < high {
print(a[high])
if (a[low] + a[high] == key ) {
return [a[low],a[high]]
}
if (a[low] + a[high] > key ){
high -= 1
}
else {
low += 1
}
}
return nil
}
IsSumlinearSearchReturnNum(a: [1,4,4,5,6], key: 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment