Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
FP32 to INT8 Conversion
00:00
5 left

FP32 to INT8 Conversion

MediumPython

Problem

Given an FP32 tensor and a floating-point scale factor, convert the input FP32 tensor to an INT8 tensor by dividing by the scale factor and casting to int8

Asked in the HM phone screen stage. Reported follow-up: how would asymmetric quantization change the conversion if a zero point were provided?

Contract

Implement convert_to_int8(tensor, scale). tensor is a non-empty, rectangular nested list of finite floating-point values. Divide each value by the positive scale, truncate toward zero, and saturate the result to the INT8 range [-128, 127]. Preserve the tensor's shape and return nested lists of Python integers.

Constraints

  • Tensor rank is at least 1.
  • All nested lists are rectangular.
  • The tensor contains at most 1000 floating-point elements.
  • Every tensor value is finite.
  • scale > 0.
  • Casting truncates toward zero.
  • Results are saturated to the inclusive INT8 range [-128, 127].

Function Signature

def convert_to_int8(tensor, scale):
Interviewer

Your question is FP32 to INT8 Conversion. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.