Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters.
"a puppy has 2 eyes 4 legs" is a sentence with seven tokens: "2" and "4" are numbers and the other tokens such as "puppy" are words.Given a string s representing a sentence, you need to check if all the numbers in s are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in s).
Return true if so, or false otherwise.
Example 1:
Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles" Output: true Explanation: The numbers in s are: 1, 3, 4, 6, 12. They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
Example 2:
Input: s = "hello world 5 x 5" Output: false Explanation: The numbers in s are: 5, 5. They are not strictly increasing.
Example 3:
Input: s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s" Output: false Explanation: The numbers in s are: 7, 51, 50, 60. They are not strictly increasing.
Constraints:
3 <= s.length <= 200s consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive.s is between 2 and 100, inclusive.s are separated by a single space.s.s is a positive number less than 100, with no leading zeros.s contains no leading or trailing spaces.Problem summary: A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters. For example, "a puppy has 2 eyes 4 legs" is a sentence with seven tokens: "2" and "4" are numbers and the other tokens such as "puppy" are words. Given a string s representing a sentence, you need to check if all the numbers in s are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in s). Return true if so, or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"1 box has 3 blue 4 red 6 green and 12 yellow marbles"
"hello world 5 x 5"
"sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s"
string-to-integer-atoi)sorting-the-sentence)check-if-all-as-appears-before-all-bs)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2042: Check if Numbers Are Ascending in a Sentence
class Solution {
public boolean areNumbersAscending(String s) {
int pre = 0;
for (var t : s.split(" ")) {
if (t.charAt(0) <= '9') {
int cur = Integer.parseInt(t);
if (pre >= cur) {
return false;
}
pre = cur;
}
}
return true;
}
}
// Accepted solution for LeetCode #2042: Check if Numbers Are Ascending in a Sentence
func areNumbersAscending(s string) bool {
pre := 0
for _, t := range strings.Split(s, " ") {
if t[0] <= '9' {
cur, _ := strconv.Atoi(t)
if pre >= cur {
return false
}
pre = cur
}
}
return true
}
# Accepted solution for LeetCode #2042: Check if Numbers Are Ascending in a Sentence
class Solution:
def areNumbersAscending(self, s: str) -> bool:
pre = 0
for t in s.split():
if t[0].isdigit():
if (cur := int(t)) <= pre:
return False
pre = cur
return True
// Accepted solution for LeetCode #2042: Check if Numbers Are Ascending in a Sentence
impl Solution {
pub fn are_numbers_ascending(s: String) -> bool {
let mut pre = -1;
for cur in s.split(' ') {
if cur.as_bytes()[0] <= b'9' {
let num = cur.parse::<i32>().unwrap();
if num <= pre {
return false;
}
pre = num;
}
}
true
}
}
// Accepted solution for LeetCode #2042: Check if Numbers Are Ascending in a Sentence
function areNumbersAscending(s: string): boolean {
let pre = -1;
for (const cur of s.split(' ')) {
if (cur[0] <= '9') {
const num = Number(cur);
if (num <= pre) {
return false;
}
pre = num;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.