Home › Interview Questions › Write a program that iterates over the numbers 100…

Write a program that iterates over the numbers 1000 to 9999, converts each number to a string, and checks if any digit repeats consecutively. If a number has no consecutive repeated digits, print it.

🟡 Medium Coding Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

The solution iterates through the range and uses a generator expression to compare each digit with its successor. The all() function short‑circuits on the first repeated pair, keeping the check efficient.

for num in range(1000, 10000):
    s = str(num)
    if all(s[i] != s[i+1] for i in range(len(s)-1)):
        print(num)

Complexity: The loop runs 9000 times, and each check examines at most 3 digit comparisons, so the overall time is O(n) with n = 9000. Memory usage is O(1) aside from the string representation of each number. This approach is clear, concise, and suitable for a junior-level candidate.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500