Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debug and Fix Provided Code

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

Your question is Debug and Fix Provided Code. 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

Identify bugs in a given piece of code and fix them.

Asked in the Technical Interview stage. Code review exercise.

Task

You are given a Python function that is intended to return the first duplicate value in a list of integers, or -1 if no duplicate exists. The current implementation contains bugs.

Write a corrected version of the function with the same signature:

def first_duplicate(nums):

The function must return the first value that appears more than once when scanning from left to right. If no value repeats, return -1.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Return the first repeated value by scan order, not the most frequent value

Function Signature

def first_duplicate(nums):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output