Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Image Rotation Coding

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

Your question is Image Rotation Coding. Start with the requirements on the right.

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.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output