Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

API Code Efficiency Review

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

Your question is API Code Efficiency Review. 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

Paycom's payroll API exposes an endpoint that calculates a pay period's gross pay for every employee on a client's account, factoring in overtime and per-employee deduction plans, before the numbers are shown to a payroll admin for approval.

package payroll

import "fmt"

type Employee struct {
	ID           string
	HourlyRate   float64
	HoursWorked  float64
	DeductionIDs []string
}

func CalculateGrossPay(employees []Employee, db *Database) map[string]float64 {
	results := map[string]float64{}

	for _, emp := range employees {
		var deductionTotal float64
		for _, id := range emp.DeductionIDs {
			deduction := db.GetDeduction(id)
			deductionTotal += deduction.Amount
		}

		var pay float64
		if emp.HoursWorked > 40 {
			regularHours := 40.0
			overtimeHours := emp.HoursWorked - 40
			pay = regularHours*emp.HourlyRate + overtimeHours*emp.HourlyRate
		} else {
			pay = emp.HoursWorked * emp.HourlyRate
		}

		pay = pay - deductionTotal
		results[emp.ID] = pay
		fmt.Println("calculated pay for", emp.ID)
	}

	return results
}

Explain what this function is doing, then identify what makes it slow and incorrect at scale, and how you would make it both faster and correct for a client with thousands of employees.