Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
For Loop as Do-While
00:00
5 left

For Loop as Do-While

EasyPython

Problem

A utility in the Teradata Vantage tooling ecosystem must repeatedly process a counter. Implement a function that produces the values visited by a do-while loop, but the implementation itself must use a for loop.

The loop body must execute at least once, even when start is already beyond limit. After each value is recorded, update the counter by step, then continue only while the updated counter remains within the limit.

Formal Specification

Implement do_while_sequence(start, step, limit).

  • Input: three integers, start, step, and limit.
  • Output: a list of integers containing every counter value processed, including the first value.
  • For a positive step, continue while the updated counter is less than or equal to limit.
  • For a negative step, continue while the updated counter is greater than or equal to limit.
  • The function must use a for loop and must not use a while loop.

Constraints

  • -10^6 <= start, limit <= 10^6
  • -10^5 <= step <= 10^5
  • step != 0
  • The resulting sequence contains at most 10^5 values

Function Signature

def do_while_sequence(start, step, limit):
Interviewer

Your question is For Loop as Do-While. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.