Numbers Without Consecutive Binary Ones
Updated
Problem statement
Return all positive integers from 1 through upperBound, inclusive, whose usual binary representation has no two consecutive 1 bits. Return them as integers in increasing numeric order, with each integer appearing once.
For example, 5 is allowed because its binary representation is 101; 6 is excluded because its representation is 110. Do not include 0 in the result, and do not return only a count or fixed-length padded bitstrings.
For this exercise, the parameter is an inclusive numeric upper bound: for example, N=15 covers values up to 15. A bound of 0 is an explicit extension that returns an empty array.
Function
numbersWithoutConsecutiveOnes(upperBound: int) → int[]Examples
Example 1
upperBound = 15return = [1,2,4,5,8,9,10]The valid positive values through 15 are 1,2,4,5,8,9,10, whose binary forms are 1,10,100,101,1000,1001,1010.
Example 2
upperBound = 5return = [1,2,4,5]Values 1,2,4,5 have binary forms 1,10,100,101. Value 3 has binary form 11 and is excluded. The inclusive upper bound allows 5 itself.
Constraints
- For this exercise, assume 0 <= upperBound <= 10000.
More Morgan Stanley Online Assessment questions
- Three-Resource 0/1 KnapsackOA · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
- Count Shipment SchedulesOA · Seen Apr 2026
- Delete and EarnOA · Seen Apr 2026
- Interleave Array HalvesOA · Seen Apr 2026
- Get Patch SequenceOA · Seen Feb 2025
- String Subsequences 🐳OA · Seen Mar 2024
- Binary Tree Level OrderOA · Seen Feb 2024
- Merge IntervalsOA · Seen Feb 2024
Similar questions at other companies
- Competitive ForagingAkuna Capital · OAAkuna Capital interview questions
- Escape the Haunted Castle with TreasuresPersona · OAPersona interview questions
- Shortest Round Trip Through All DeliveriesGoogle · ONSITE INTERVIEWGoogle interview questions
- Car to Oasis with Gas StationsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Shortest Path Visiting All NodesInMobi · ONSITE INTERVIEWInMobi interview questions
- Minimum Distance Matching in a ForestScale AI · PHONE SCREEN · ONSITE INTERVIEWScale AI interview questions
- Minimum-Sum Tree Fill with Unit StepsVisa · OAVisa interview questions
- Knights, Knaves and Their StatementsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Minimum N-ary Tree Depth DeletionsSnowflake · OA · PHONE SCREENSnowflake interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)