FastPrepStrict Text to Integer for a Given Size
Problems/Strict Text to Integer for a Given Size

Strict Text to Integer for a Given Size

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem turns text into a signed integer under strict rules: surrounding spaces are ignored, the rest must be digits with at most a minus in front, and anything else, or a value outside the integer size, gives the largest value of that size. An Adobe CS-1 report lists a conversion like this as its second first-round coding question: it gives the space and digit rules, asks for the largest integer when a number overflows, and warns that the integer size depends on the compiler (2 or 4 bytes).

Our choices: invalid text and numbers below the smallest value also give the largest value; the size comes in as bits = 16 or 32; only the space character is trimmed; a plus sign, empty text or a lone minus is invalid; leading zeros are allowed; at most 200 characters; and new examples.

The judged task matches the report at about 85%.

Convert the text s into a signed integer of size bits, whose values run from -2^(bits-1) to 2^(bits-1) - 1 (for 16 bits: -32768 to 32767; for 32 bits: -2147483648 to 2147483647).

  • Spaces at the start and end are ignored; no other character is trimmed.
  • What is left is valid only if it is one or more digits, optionally after a single leading -. Leading zeros are fine.
  • If the text is not valid, or its value does not fit the size, return the largest value of the size, 2^(bits-1) - 1.

Otherwise return the value. Your code should work for either size, without assuming how big an integer is.

Function

parseStrict(s: String, bits: int) → int

Examples

Example 1

s = "  -2048 "bits = 16return = -2048

The spaces around the text are ignored. -2048 is a minus sign followed by digits, and it fits in 16 bits (from -32768 to 32767), so the answer is -2048.

Example 2

s = "40000"bits = 16return = 32767

40000 is above 32767, the largest 16-bit value, so the answer is 32767. With bits = 32 the same text would give 40000.

Example 3

s = "12 34"bits = 32return = 2147483647

A space inside the number makes it invalid, so the answer is the largest 32-bit value, 2147483647.

Constraints

  • 0 <= s.length <= 200
  • s contains printable ASCII characters only (codes 32 to 126)
  • bits is 16 or 32

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int parseStrict(String s, int bits) {
    // Write your code here.
}
Checking account…