Your question is Refactoring for Performance. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Evident ID's identity verification service screens every applicant against a watchlist before issuing a decision, and this function is the hot path that runs on every single verification request. It works, but under load it has become one of the slowest services in the pipeline.
package main
import "fmt"
import "regexp"
import "strings"
type Applicant struct {
ID string
FullName string
DOB string
}
type WatchlistEntry struct {
Name string
DOB string
}
func screenApplicants(applicants []Applicant, watchlist []WatchlistEntry) []string {
var flagged []string
for _, applicant := range applicants {
namePattern := regexp.MustCompile(`(?i)^` + applicant.FullName + `$`)
matched := false
for _, entry := range watchlist {
if namePattern.MatchString(entry.Name) && entry.DOB == applicant.DOB {
matched = true
}
}
if matched {
summary := ""
summary = summary + "Applicant " + applicant.ID + " matched watchlist entry for " + applicant.FullName
flagged = append(flagged, summary)
}
}
return flagged
}
func main() {
applicants := []Applicant{
{ID: "A1", FullName: "Jordan Ellis", DOB: "1990-04-12"},
}
watchlist := []WatchlistEntry{
{Name: "Jordan Ellis", DOB: "1990-04-12"},
}
results := screenApplicants(applicants, watchlist)
fmt.Println(strings.Join(results, "
"))
}
Refactor this snippet to improve performance for a batch of thousands of applicants screened against a watchlist of tens of thousands of entries. Name each slow pattern, explain its real cost at that scale, and describe what you would replace it with. Flag anything you'd consider a correctness or security problem as well, not just a speed one.