List the integers in a range that are divisible by 5 or 7

Quick Overview

Return every integer in an inclusive range that is divisible by 5 or 7, in ascending order and with no repeats. The problem tests careful loop bounds, how the modulo operator treats negative numbers and zero, and avoiding duplicates for numbers divisible by both.

List the integers in a range that are divisible by 5 or 7

Company: DXC Technology

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two integers `a` and `b` that describe an inclusive range, return a list of all integers in `[a, b]` that are divisible by 5 or 7 (or both), in ascending order. An integer `x` is divisible by `k` if `x % k == 0`. The result should contain each qualifying integer exactly once, sorted in ascending order. If no integer in the range qualifies, return an empty list. ### Function Signature ```python def divisible_by_five_or_seven(a: int, b: int) -> list[int]: ``` ### Rules - Both ends of the range are included. - An integer divisible by both 5 and 7 (such as 35 or 0) appears once. - Negative integers and zero follow the same rule, so `-14` and `0` both qualify. - The list is in ascending order. ### Constraints - `-10^6 <= a <= b <= 10^6` - `b - a + 1 <= 2 * 10^6` ### Examples ```text Input: a = 1, b = 20 Output: [5, 7, 10, 14, 15, 20] ``` The multiples of 5 in the range are 5, 10, 15 and 20, and the multiples of 7 are 7 and 14. Combined in ascending order they give the output. ```text Input: a = 30, b = 40 Output: [30, 35, 40] ``` The multiples of 5 are 30, 35 and 40. The only multiple of 7 is 35, which is already in the list, so it appears once. ```text Input: a = -10, b = 0 Output: [-10, -7, -5, 0] ``` Negative multiples count: -10 and -5 are multiples of 5, -7 is a multiple of 7, and 0 is a multiple of both.

Overview: Return every integer in an inclusive range that is divisible by 5 or 7, in ascending order and with no repeats. The problem tests careful loop bounds, how the modulo operator treats negative numbers and zero, and avoiding duplicates for numbers divisible by both.

DXC Technology logo
DXC Technology
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two integers a and b that describe an inclusive range, return a list of all integers in [a, b] that are divisible by 5 or 7 (or both), in ascending order.

An integer x is divisible by k if x % k == 0. The result should contain each qualifying integer exactly once, sorted in ascending order. If no integer in the range qualifies, return an empty list.

Function Signature

def divisible_by_five_or_seven(a: int, b: int) -> list[int]:

Rules

  • Both ends of the range are included.
  • An integer divisible by both 5 and 7 (such as 35 or 0) appears once.
  • Negative integers and zero follow the same rule, so -14 and 0 both qualify.
  • The list is in ascending order.

Constraints

  • -10^6 <= a <= b <= 10^6
  • b - a + 1 <= 2 * 10^6

Examples

Input: a = 1, b = 20
Output: [5, 7, 10, 14, 15, 20]

The multiples of 5 in the range are 5, 10, 15 and 20, and the multiples of 7 are 7 and 14. Combined in ascending order they give the output.

Input: a = 30, b = 40
Output: [30, 35, 40]

The multiples of 5 are 30, 35 and 40. The only multiple of 7 is 35, which is already in the list, so it appears once.

Input: a = -10, b = 0
Output: [-10, -7, -5, 0]

Negative multiples count: -10 and -5 are multiples of 5, -7 is a multiple of 7, and 0 is a multiple of both.