Odd Numbers Up and Back
Updated
Problem statement
You are given two whole numbers start and end with start <= end.
List the odd numbers from start up to end, and then come back down to start. More exactly, let o_1 < o_2 < ... < o_k be the odd numbers between start and end (both included). Return o_1, o_2, ..., o_k followed by o_(k-1), ..., o_2, o_1 (the number at position k - 1, not o_k minus 1). The largest odd number o_k appears only once.
If start or end is even, it is not listed. If there is no odd number in the range, return an empty list.
Interview note: the interview version allowed no global variables, no helper functions and no loops, so it had to be one recursive function. The judge only checks the returned list, so any method is accepted here. The editorial shows the recursive version.
Function
oddUpAndBack(start: int, end: int) → int[]Examples
Example 1
start = 3end = 9return = [3,5,7,9,7,5,3]The odd numbers from 3 to 9 are 3, 5, 7, 9. After reaching 9 the list goes back down through 7, 5, 3. The top value 9 is listed once, so the answer has 7 numbers.
Example 2
start = 4end = 10return = [5,7,9,7,5]4 is even, so the list starts at 5. 10 is also even, so the top odd value is 9. Up gives 5, 7, 9 and down gives 7, 5.
Example 3
start = 6end = 6return = []The only number in the range is 6, which is even. There is no odd number, so the answer is an empty list.
Constraints
0 <= start <= end <= 2000
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Checked Recursive SumHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Factorial Without MultiplicationWayve · PHONE SCREEN
- Power with Integer ExponentsNVIDIA · PHONE SCREENNVIDIA interview questions
- Raise a Number to an Integer PowerAdobe · PHONE SCREENAdobe interview questions
- Pow(x, n)Bloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Preorder of a Special Binary TreeNielsen · OA
- Multiply Without the Multiplication OperatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Fibonacci with Input ValidationMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- FactorialAMD · ONSITE INTERVIEWAMD interview questions
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions