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

Refactor for Edge Cases

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

Whenever you're ready, walk me through how you'd approach this.

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

Problem

Dropbox's sync client computes which content-addressed chunks of a file still need to be uploaded by comparing a local chunk manifest against what the server already has. The function below works for the common case in manual testing, but it's been flaky on the edge cases the sync team cares about: brand-new empty files, files with duplicate chunks, and manifests that arrive with inconsistent hash casing from older clients.

package sync

import "strings"

type ChunkManifest struct {
	FileID string
	Hashes []string
}

func ChunksToUpload(local ChunkManifest, remote ChunkManifest) []string {
	toUpload := []string{}

	for i := 0; i < len(local.Hashes); i++ {
		hash := local.Hashes[i]
		found := false
		for j := 0; j < len(remote.Hashes); j++ {
			if strings.ToLower(hash) == remote.Hashes[j] {
				found = true
			}
		}
		if !found {
			toUpload = append(toUpload, hash)
		}
	}

	first := local.Hashes[0]
	toUpload = append(toUpload, "manifest-root:"+first)

	return toUpload
}

Refactor this so it handles those edge cases correctly and reads more clearly. Explain what you'd change and why, rather than only rewriting the whole file.