Final value after doubling a number each time a left-to-right array scan finds it

Quick Overview

Scan an integer array from left to right, double a running value each time the current element equals it, and return the final value. It tests careful step-by-step simulation, handling of zero and negative starting values, and why the order of the elements changes the result.

Final value after doubling a number each time a left-to-right array scan finds it

Company: Flipkart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums` and an initial integer `b`, traverse `nums` from left to right. For each element, if it equals the current value of `b`, double `b` (set `b = b * 2`). Otherwise, leave `b` unchanged. Return the final value of `b` after processing every element of the array. ### Function Signature ```python def final_value(nums: list[int], b: int) -> int: ``` ### Rules - Each element is compared with the value `b` has when the scan reaches that element. Elements already passed are never checked again after `b` changes. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` - `-10^9 <= b <= 10^9` ### Examples ```text Input: nums = [2,4,8,16], b = 2 Output: 32 ``` Start with `b = 2`. Seeing 2 makes it 4, seeing 4 makes it 8, seeing 8 makes it 16, and seeing 16 makes it 32. ```text Input: nums = [1,3,5,7], b = 2 Output: 2 ``` No element ever equals 2, so `b` stays 2. ```text Input: nums = [8,2,4,2,8], b = 2 Output: 16 ``` The first 8 is passed while `b` is still 2, so it does not count. Then 2 makes `b` 4, 4 makes it 8, the second 2 does not match, and the last 8 makes it 16.

Overview: Scan an integer array from left to right, double a running value each time the current element equals it, and return the final value. It tests careful step-by-step simulation, handling of zero and negative starting values, and why the order of the elements changes the result.

Flipkart logo
Flipkart
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums and an initial integer b, traverse nums from left to right.

For each element, if it equals the current value of b, double b (set b = b * 2). Otherwise, leave b unchanged.

Return the final value of b after processing every element of the array.

Function Signature

def final_value(nums: list[int], b: int) -> int:

Rules

  • Each element is compared with the value b has when the scan reaches that element. Elements already passed are never checked again after b changes.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= b <= 10^9

Examples

Input: nums = [2,4,8,16], b = 2
Output: 32

Start with b = 2. Seeing 2 makes it 4, seeing 4 makes it 8, seeing 8 makes it 16, and seeing 16 makes it 32.

Input: nums = [1,3,5,7], b = 2
Output: 2

No element ever equals 2, so b stays 2.

Input: nums = [8,2,4,2,8], b = 2
Output: 16

The first 8 is passed while b is still 2, so it does not count. Then 2 makes b 4, 4 makes it 8, the second 2 does not match, and the last 8 makes it 16.