Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Longest Subarray After Removals

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

Your question is Longest Subarray After Removals. 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

FactSet analytics surfaces may compact repeated values after filtering records. Given an integer array nums and an integer k, return the maximum length of a contiguous subarray consisting of the same integer after deleting at most k elements. The relative order of the remaining elements must be preserved.

You may choose any value and delete elements between its occurrences so that the retained occurrences become consecutive. Deleted elements do not contribute to the returned length.

Formal Specification

Implement longest_equal_run(nums, k).

  • Input: nums, a list of integers, and k, a non-negative integer.
  • Output: an integer representing the greatest possible length of a run containing only one repeated value after at most k deletions.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= k <= len(nums)
  • -10^9 <= nums[i] <= 10^9

Function Signature

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