Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Last Man Standing Stream

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

Your question is Last Man Standing Stream. 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

Zillow processes a circular stream of listing IDs for a controlled rotation. Starting at the first listing, count step remaining listings, remove the counted listing, and continue counting from the next remaining listing. Return the listing ID that remains last.

Avoid simulating removals with repeated list deletion, since the input can be large. Derive and implement an efficient solution based on the Josephus recurrence.

Formal Specification

Implement last_remaining(listing_ids, step).

  • listing_ids is a non-empty list of distinct integers in stream order.
  • step is a positive integer.
  • Counting begins at index 0, and the first removed listing is the stepth listing, wrapping around as necessary.
  • Return the integer ID of the final remaining listing.

Constraints

  • 1 <= len(listing_ids) <= 10^6
  • 1 <= step <= 10^12
  • All listing IDs are distinct integers
  • Use O(1) auxiliary space beyond the input

Function Signature

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