Created
September 15, 2015 14:25
-
-
Save SteveBate/c4dba3582fa09cb27373 to your computer and use it in GitHub Desktop.
One possible implementation of a facebook interview question that requires the developer to take a list of users and attempt to union those records that appear to represent the same user. In the sample data, user rows 0, 2, and 3 are one user and row 1 is another therefore: Given an input of these four rows, the output should consist of only 2 rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type contact struct { | |
name string | |
phone string | |
email string | |
} | |
func main() { | |
source := make([]contact, 0, 10) | |
source = append(source, contact{"Joe Bloggs", "555451", "joe@aol.com"}) | |
source = append(source, contact{"Jo Bloggs", "347012", "joe@compuserve.com"}) | |
source = append(source, contact{"J Bloggs", "555451", "joe@msn.com"}) | |
source = append(source, contact{"Joeseph Bloggs", "457679", "joe@aol.com"}) | |
output := make(map[int][]contact) | |
matches := make([]int, 0, 10) | |
for x, _ := range source { | |
for y, _ := range source { | |
if !contains(matches, y) { | |
if source[y].phone == source[x].phone || source[y].email == source[x].email { | |
//fmt.Printf("match: %d - %d %s\n", x, y, source[y].email) | |
if slice, exists := output[x]; !exists { | |
slice = make([]contact, 0, 10) | |
slice = append(slice, source[y]) | |
output[x] = slice | |
} else { | |
output[x] = append(output[x], source[y]) | |
} | |
matches = append(matches, y) | |
} | |
} | |
} | |
} | |
for _, v := range output { | |
fmt.Println(v) | |
} | |
} | |
func contains(values []int, x int) bool { | |
for _, v := range values { | |
if x == v { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment