Skip to content

Instantly share code, notes, and snippets.

@caelifer
Created January 7, 2020 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/6af0ef7b74e955cb7a44773430a43edb to your computer and use it in GitHub Desktop.
Save caelifer/6af0ef7b74e955cb7a44773430a43edb to your computer and use it in GitHub Desktop.
Figure out if the phone number is correct without asking for it directly.
// This program provides an answer for one of the Google interview questions described here:
// https://www.businessinsider.com/answers-to-google-interview-questions-2011-11#you-need-to-check-that-your-friend-bob-has-your-correct-phone-number-10
package main
import (
"fmt"
"strings"
)
func main() {
collisions := make(map[uint][]string)
for _, p := range [...][10]rune{
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '2'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '3'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '4'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '5'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '6'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '7'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '8'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '9'},
{'2', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'1', '2', '1', '1', '1', '1', '1', '1', '1', '1'},
{'1', '1', '2', '1', '1', '1', '1', '1', '1', '1'},
{'1', '1', '1', '2', '1', '1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '2', '1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1', '2', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1', '1', '2', '1', '1', '1'},
{'1', '1', '1', '1', '1', '1', '1', '2', '1', '1'},
{'1', '1', '1', '1', '1', '1', '1', '1', '2', '1'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '2'},
{'9', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'1', '9', '1', '1', '1', '1', '1', '1', '1', '1'},
{'1', '1', '9', '1', '1', '1', '1', '1', '1', '1'},
{'1', '1', '1', '9', '1', '1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '9', '1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1', '9', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1', '1', '9', '1', '1', '1'},
{'1', '1', '1', '1', '1', '1', '1', '9', '1', '1'},
{'1', '1', '1', '1', '1', '1', '1', '1', '9', '1'},
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '9'},
} {
phone, sum := Phone(p), Sum(p)
fmt.Printf("%s - %d\n", phone, sum)
if phones, ok := collisions[sum]; ok {
phones = append(phones, phone)
collisions[sum] = phones
} else {
collisions[sum] = []string{phone}
}
}
for k, v := range collisions {
if len(v) > 1 {
fmt.Printf("Found collision on %d for: %s\n", k, strings.Join(v, ", "))
}
}
}
func Sum(digits [10]rune) uint {
res := uint(0)
for _, n := range digits {
res += (res + uint(n-'0')) * 10
}
return res
}
func Phone(digits [10]rune) string {
return fmt.Sprintf("(%s) %s-%s", string(digits[:3]), string(digits[3:6]), string(digits[6:]))
}
@caelifer
Copy link
Author

caelifer commented Jan 7, 2020

Live code - https://play.golang.org/p/HWtXPNNO22B

Output:

(111) 111-1111 - 167620824
(111) 111-1112 - 167620834
(111) 111-1113 - 167620844
(111) 111-1114 - 167620854
(111) 111-1115 - 167620864
(111) 111-1116 - 167620874
(111) 111-1117 - 167620884
(111) 111-1118 - 167620894
(111) 111-1119 - 167620904
(211) 111-1111 - 2272261254
(121) 111-1111 - 2311209634
(112) 111-1111 - 362492534
(111) 211-1111 - 185336434
(111) 121-1111 - 169231334
(111) 112-1111 - 167767234
(111) 111-2111 - 167634134
(111) 111-1211 - 167622034
(111) 111-1121 - 167620934
(111) 111-1112 - 167620834
(911) 111-1111 - 4119842376
(191) 111-1111 - 136462120
(119) 111-1111 - 1726594504
(111) 911-1111 - 309345704
(111) 191-1111 - 180504904
(111) 119-1111 - 168792104
(111) 111-9111 - 167727304
(111) 111-1911 - 167630504
(111) 111-1191 - 167621704
(111) 111-1119 - 167620904
Found collision on 167620834 for: (111) 111-1112, (111) 111-1112
Found collision on 167620904 for: (111) 111-1119, (111) 111-1119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment