binary string
shortest string
algorithm
string interval
optimization

Find Shortest Binary String In Given Interval

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If a binary string represents an integer in the usual way with no leading zeroes, then the shortest binary string in an interval is simply the representation of a number in that interval with the smallest bit length. That turns the problem from "searching strings" into a much simpler question about binary length boundaries.

Restating the Problem Carefully

Suppose the interval is [a, b] with 0 <= a <= b, and binary strings are written in standard form:

  • '0 is written as 0'
  • positive numbers have no leading zeroes

Then the length of the binary representation of a positive integer x is:

floor(log2(x)) + 1

That means every integer in the range:

  • '1 to 1 has length 1'
  • '2 to 3 has length 2'
  • '4 to 7 has length 3'
  • '8 to 15 has length 4'

So the shortest binary string in [a, b] is any number from the interval that lies in the smallest bit-length bucket touched by that interval.

The Key Observation

If a and b have the same bit length, then every integer in the interval has the same binary string length. In that case, there may be many valid answers, but none is shorter than the others.

If the interval spans a power-of-two boundary, the answer comes from the smaller-length side.

For example:

  • interval [10, 15] gives length 4 for every value
  • interval [7, 20] includes 7, whose binary form 111 has length 3, while most later values have length 4 or 5
  • interval [0, 9] includes 0, so the shortest string is simply 0

This means you do not need to enumerate every binary string in the interval.

A Direct Algorithm

The simplest algorithm is:

  1. if 0 lies in the interval, return 0
  2. otherwise compute the bit length of a
  3. find the smallest number in the interval having that bit length
  4. return its binary representation

In standard non-negative intervals, that number is just a itself, because all integers from a upward have length at least the length of a.

So under the usual interpretation, the shortest binary string in [a, b] is the binary representation of a, unless 0 is in the interval and you want the one-character answer 0.

Python Implementation

Here is a straightforward implementation:

python
1def shortest_binary_string(a: int, b: int) -> str:
2    if a < 0 or b < 0 or a > b:
3        raise ValueError("expected 0 <= a <= b")
4
5    if a == 0:
6        return "0"
7
8    return bin(a)[2:]
9
10print(shortest_binary_string(10, 15))
11print(shortest_binary_string(7, 20))
12print(shortest_binary_string(0, 9))

This works because a is already the smallest number in the interval, and the smallest number in a non-negative interval also has the smallest binary length.

If You Want Only the Length

Sometimes the real task is to find the minimum length rather than the actual string. That can be computed without building the string at all:

python
1def shortest_binary_length(a: int, b: int) -> int:
2    if a < 0 or b < 0 or a > b:
3        raise ValueError("expected 0 <= a <= b")
4
5    if a == 0:
6        return 1
7
8    return a.bit_length()
9
10print(shortest_binary_length(10, 15))
11print(shortest_binary_length(0, 9))

bit_length() is the cleanest tool for this in Python.

Why the Problem Sometimes Looks Harder Than It Is

This problem can appear more complicated because people start thinking in terms of string search, lexicographic order, or generating all binaries between two endpoints. That is usually unnecessary.

The numerical meaning of the binary string is what matters. Once you frame the problem numerically, the answer follows from the monotonic growth of bit length:

  • larger non-negative integers never have shorter standard binary representations than smaller ones

That property collapses the search space immediately.

A Variation: Allowing Leading Zeroes

If leading zeroes were allowed and string length were still being minimized, the problem becomes ambiguous or trivial depending on the exact rules. For example, 00101 and 101 represent the same number but have different lengths.

That is why the standard interpretation forbids leading zeroes for positive integers. Without that assumption, “shortest binary string” is not a well-posed question.

Common Pitfalls

The biggest mistake is overcomplicating the problem by enumerating every value in the interval. For standard binary representations of non-negative integers, the answer is determined by the smallest value.

Another issue is forgetting to define whether leading zeroes are allowed. Most algorithm problems assume they are not.

People also sometimes confuse the shortest numeric representation with lexicographically smallest binary string. Those are different objectives.

Finally, if negative numbers are allowed, the problem changes completely because binary representation then depends on conventions such as sign bits or two's complement width.

Summary

  • For standard non-negative binary representations, shorter strings correspond to smaller bit lengths.
  • In an interval [a, b] with no leading zeroes, the shortest binary string is the representation of the smallest value, namely a.
  • If a is 0, the answer is 0 with length 1.
  • You do not need to enumerate the interval to solve the standard problem.
  • Always clarify whether leading zeroes or negative numbers are part of the representation model.

Course illustration
Course illustration

All Rights Reserved.