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.
Given a date string in the form Day Month Year, where:
Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}.Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.Year is in the range [1900, 2100].Convert the date string to the format YYYY-MM-DD, where:
YYYY denotes the 4 digit year.MM denotes the 2 digit month.DD denotes the 2 digit day.Example 1:
Input: date = "20th Oct 2052" Output: "2052-10-20"
Example 2:
Input: date = "6th Jun 1933" Output: "1933-06-06"
Example 3:
Input: date = "26th May 1960" Output: "1960-05-26"
Constraints:
Problem summary: Given a date string in the form Day Month Year, where: Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}. Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}. Year is in the range [1900, 2100]. Convert the date string to the format YYYY-MM-DD, where: YYYY denotes the 4 digit year. MM denotes the 2 digit month. DD denotes the 2 digit day.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"20th Oct 2052"
"6th Jun 1933"
"26th May 1960"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1507: Reformat Date
class Solution {
public String reformatDate(String date) {
var s = date.split(" ");
String months = " JanFebMarAprMayJunJulAugSepOctNovDec";
int day = Integer.parseInt(s[0].substring(0, s[0].length() - 2));
int month = months.indexOf(s[1]) / 3 + 1;
return String.format("%s-%02d-%02d", s[2], month, day);
}
}
// Accepted solution for LeetCode #1507: Reformat Date
func reformatDate(date string) string {
s := strings.Split(date, " ")
day, _ := strconv.Atoi(s[0][:len(s[0])-2])
months := " JanFebMarAprMayJunJulAugSepOctNovDec"
month := strings.Index(months, s[1])/3 + 1
year, _ := strconv.Atoi(s[2])
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
}
# Accepted solution for LeetCode #1507: Reformat Date
class Solution:
def reformatDate(self, date: str) -> str:
s = date.split()
s.reverse()
months = " JanFebMarAprMayJunJulAugSepOctNovDec"
s[1] = str(months.index(s[1]) // 3 + 1).zfill(2)
s[2] = s[2][:-2].zfill(2)
return "-".join(s)
// Accepted solution for LeetCode #1507: Reformat Date
struct Solution;
use std::collections::HashMap;
impl Solution {
fn reformat_date(date: String) -> String {
let month: HashMap<String, i32> = vec![
("Jan", 1),
("Feb", 2),
("Mar", 3),
("Apr", 4),
("May", 5),
("Jun", 6),
("Jul", 7),
("Aug", 8),
("Sep", 9),
("Oct", 10),
("Nov", 11),
("Dec", 12),
]
.into_iter()
.map(|(m, mm)| (m.to_string(), mm))
.collect();
let mut v: Vec<String> = date.split_whitespace().map(|s| s.to_string()).collect();
let yyyy = v.pop().unwrap();
let mm = month[&v.pop().unwrap()];
let mut dd = v.pop().unwrap();
dd.pop();
dd.pop();
let dd = dd.parse::<i32>().unwrap();
format!("{}-{:02}-{:02}", yyyy, mm, dd)
}
}
#[test]
fn test() {
let date = "20th Oct 2052".to_string();
let res = "2052-10-20".to_string();
assert_eq!(Solution::reformat_date(date), res);
let date = "6th Jun 1933".to_string();
let res = "1933-06-06".to_string();
assert_eq!(Solution::reformat_date(date), res);
let date = "26th May 1960".to_string();
let res = "1960-05-26".to_string();
assert_eq!(Solution::reformat_date(date), res);
}
// Accepted solution for LeetCode #1507: Reformat Date
function reformatDate(date: string): string {
const s = date.split(' ');
const months = ' JanFebMarAprMayJunJulAugSepOctNovDec';
const day = parseInt(s[0].substring(0, s[0].length - 2));
const month = Math.floor(months.indexOf(s[1]) / 3) + 1;
return `${s[2]}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
}
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.