Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started

Optimize Time and Space

HardCoding00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimize Time and Space. 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).

You need to log in / sign up to chat or submit.

Problem

IMDB-style services aggregate millions of user ratings into per-title averages for display and ranking. The Go code below produces correct numbers but does not scale to a large catalog of titles and ratings.

package main

import "fmt"
import "strconv"

type Movie struct {
    ID    string
    Title string
}

type Rating struct {
    MovieID string
    Score   float64
}

func buildReport(movies []Movie, ratings []Rating) string {
    report := ""
    for _, m := range movies {
        var total float64
        var count int
        for _, r := range ratings {
            if r.MovieID == m.ID {
                total += r.Score
                count++
            }
        }
        var avg float64
        if count > 0 {
            avg = total / float64(count)
        }
        report = report + m.Title + ": " + strconv.FormatFloat(avg, 'f', 2, 64) + "
"
    }
    return report
}

func topRated(movies []Movie, ratings []Rating, minCount int) []string {
    var result []string
    for _, m := range movies {
        count := 0
        for _, r := range ratings {
            if r.MovieID == m.ID {
                count++
            }
        }
        if count >= minCount {
            result = append(result, m.Title)
        }
    }
    return result
}

func main() {
    movies := []Movie{{"tt001", "Arrival Point"}, {"tt002", "Silent Harbor"}}
    ratings := []Rating{{"tt001", 8.0}, {"tt001", 9.0}, {"tt002", 7.0}}
    fmt.Println(buildReport(movies, ratings))
    fmt.Println(topRated(movies, ratings, 2))
}

Explain the time and space complexity of buildReport and topRated as written, and how you would bring them down for a catalog with millions of titles and ratings.