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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Image Rotation Coding
00:00
5 left

Image Rotation Coding

HardPython

Problem

Tesla Tecnologia e Comunicação's computer vision pipeline receives grayscale images that must be rotated before downstream processing. Implement a function that rotates an image counterclockwise by an arbitrary angle and returns the smallest rectangular output canvas containing the rotated image.

Use inverse mapping with nearest-neighbor sampling. Treat each pixel as a unit square centered at integer coordinates. Pixels in the output whose mapped source coordinate falls outside the input image must be filled with 0.

Formal Specification

The input image is a non-empty rectangular 2D list of integers, where image[y][x] is the pixel at column x and row y. The input angle is measured in degrees and may be positive, negative, or greater than 360. Return a new 2D list of integers.

Rotate around the center of the input image. The output dimensions are:

  • ceil(width * |cos(angle)| + height * |sin(angle)|) columns
  • ceil(width * |sin(angle)| + height * |cos(angle)|) rows

For every output pixel, map its center backward through the inverse rotation. Round each source coordinate to the nearest integer, and copy that pixel when it is in bounds.

Constraints

  • 1 <= height, width <= 500
  • Pixel values are integers from 0 through 255
  • -10^5 <= angle <= 10^5
  • The input image is rectangular and non-empty
  • Use nearest-neighbor sampling
  • Out-of-bounds output pixels must contain 0

Function Signature

def rotate_image(image, angle):
Interviewer

Your question is Image Rotation Coding. 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.