FastPrepValidate Input Against Length Bounds

Validate Input Against Length Bounds

Klaviyo logoKlaviyo● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

For this exercise, validate a text input against a supplied length rule. Return true exactly when the number of characters in text is at least minLength and at most maxLength; otherwise return false.

  • Both endpoints are inclusive.
  • The text contains only printable ASCII characters. Spaces and punctuation each count as one character.
  • Measure the original text without trimming, changing case or interpreting markup.
  • The supplied bounds are valid: 0 <= minLength <= maxLength.

This is the practice validation predicate for one input value. The source did not specify a validation rule, UI framework or event-wiring interface.

Function

validateInputLength(text: String, minLength: int, maxLength: int) → boolean

Examples

Example 1

text = "hello"minLength = 3maxLength = 5return = true

The text has five characters, so it satisfies the inclusive interval from three through five.

Example 2

text = ""minLength = 1maxLength = 4return = false

The empty text has length zero, below the supplied minimum of one.

Constraints

  • 0 <= text.length <= 10000.
  • Every text character has an ASCII code from 32 through 126.
  • 0 <= minLength <= maxLength <= 10000.

More Klaviyo problems

See Klaviyo hiring insights
public boolean validateInputLength(String text, int minLength, int maxLength) {
    // Write your code here
}
text"hello"
minLength3
maxLength5
expectedtrue
Checking account…