Correct Apartment Bedroom Counts
Reported by candidates from Ramp's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Ramp reported this one in August 2024, and it looks like a gentle JSON parsing task until the yoga studio shows up. You get a string of apartment listings, each with a description and a possibly wrong num_bedrooms value. Your job is to fix the value from the text and return an int array. The pattern is plain string parsing and tokenizing, with one nasty exception rule that trips a naive contains() check. If you blank on the tokenizing during the live OA, StealthCoder sits invisibly on your screen as a safety net. Know the trick first and you won't need it.
The problem
We want to query an API endpoint to receive data about currently available apartment listings from a rental website. Among the data fields is a column called num_bedrooms, which takes the value of 1 for a "1-bedroom" apartment and 0 for a "studio".
Note: This rental agency only works with studios and 1-bedroom apartments, so there will never be 2+ bedroom listings. Each listing includes information about a "studio" or a "1-bedroom" apartment, so there will never be a listing with both a "studio" and "1-bedroom" offerings in one posting.
The algorithm used occasionally mistags the num_bedrooms value. Specifically, sometimes a "studio" is tagged as having num_bedrooms = 1 or a "1-bedroom" is tagged as num_bedrooms = 0. Further investigation revealed it to be an issue with one of the data fields, description, and the way our algorithm parsed the field to extract a num_bedrooms value.
For example: "description": "Beautiful 1-bedroom apartment with nearby yoga studio." was detected as a yoga studio instead of 1-bedroom and incorrectly had num_bedrooms = 0.
Your task is to write a function that takes in the jsonData and corrects this problem. The GET request retrieves the data as a string which looks like this:
jsonData = [
{
"id": "3",
"agent": "Ton Jett",
"unit": "#12",
"description": "Beautiful 1-bedroom apartment with nearby yoga studio.",
"num_bedrooms": 1
},
...
]
While correcting the problem, remember the following edge cases:
If the word "studio" or "1-bedroom" is preceded immediately by any of the words: "yoga", "dance" or "art", don't consider it for num_bedrooms value.
If the description does not contain the word "studio" or "1-bedroom", do not change the value for num_bedrooms.
The rules above should be applied regardless of punctuation or letter casing within the description field.
Your end goal is to return an array of integers representing num_bedrooms for each rental listing, example: [0, 1, 1, 1, 0, 0].
The source labels the following case Example.
For
jsonData = "[{\"id\":\"1\",\"agent\":\"Radulf Katlego\",\"unit\":\"#3\",\"description\":\"This luxurious studio apartment is in the heart of downtown.\",\"num_bedrooms\":1},{\"id\":\"2\",\"agent\":\"Kelemen Konrad\",\"unit\":\"#36\",\"description\":\"We have a 1-bedroom available on the third floor.\",\"num_bedrooms\":1},{\"id\":\"3\",\"agent\":\"Ton Jett\",\"unit\":\"#12\",\"description\":\"Beautiful 1-bedroom apartment with nearby yoga studio.\",\"num_bedrooms\":1},{\"id\":\"4\",\"agent\":\"Fishel Salman\",\"unit\":\"#13\",\"description\":\"Beautiful studio with a nearby art studio.\",\"num_bedrooms\":1}]"
the output should be solution(jsonData) = [0, 1, 1, 0].
The above jsonData represents the following JSON:
[
{
"id": "1",
"agent": "Radulf Katlego",
"unit": "#3",
"description": "This luxurious studio apartment is in the heart of downtown.",
"num_bedrooms": 1
},
{
"id": "2",
"agent": "Kelemen Konrad",
"unit": "#36",
"description": "We have a 1-bedroom available on the third floor.",
"num_bedrooms": 1
},
{
"id": "3",
"agent": "Ton Jett",
"unit": "#12",
"description": "Beautiful 1-bedroom apartment with nearby yoga studio.",
"num_bedrooms": 1
},
{
"id": "4",
"agent": "Fishel Salman",
"unit": "#13",
"description": "Beautiful studio with a nearby art studio.",
"num_bedrooms": 1
}
]
FastPrep practice clarification
The source's two explanation sentences using followed by are retained verbatim below. For judging, follow the explicit source rule: ignore a target occurrence when its immediately preceding word is yoga, dance, or art.
Match the complete words studio and 1-bedroom case-insensitively. Punctuation separates words, so ART...studio has art as the immediately preceding word.
Return corrected values in the same order as the listings in jsonData. If no non-ignored target occurs, retain that listing's original num_bedrooms.
Function
solution(jsonData: String) → int[]
Examples
Example 1
jsonData = "[{\"id\":\"1\",\"agent\":\"Radulf Katlego\",\"unit\":\"#3\",\"description\":\"This luxurious studio apartment is in the heart of downtown.\",\"num_bedrooms\":1},{\"id\":\"2\",\"agent\":\"Kelemen Konrad\",\"unit\":\"#36\",\"description\":\"We have a 1-bedroom available on the third floor.\",\"num_bedrooms\":1},{\"id\":\"3\",\"agent\":\"Ton Jett\",\"unit\":\"#12\",\"description\":\"Beautiful 1-bedroom apartment with nearby yoga studio.\",\"num_bedrooms\":1},{\"id\":\"4\",\"agent\":\"Fishel Salman\",\"unit\":\"#13\",\"description\":\"Beautiful studio with a nearby art studio.\",\"num_bedrooms\":1}]"
return = [0, 1, 1, 0]
In the first listing, description = "This luxurious studio apartment is in the heart of downtown."
"studio" should have num_bedrooms = 0;
In the second listing, description = "We have a 1-bedroom available on the third floor."
"1-bedroom" should have num_bedrooms = 1;
In the third listing, description = "Beautiful 1-bedroom apartment with nearby yoga studio."
"1-bedroom" should have num_bedrooms = 1. Ignore "studio" since it is followed by "yoga".
In the fourth listing, description = "Beautiful studio with a nearby art studio."
"studio" should have num_bedrooms = 0. Ignore the second appearance of "studio" since it is followed by "art".
Example 2
jsonData = "[{\"id\":\"5\",\"agent\":\"Ari Lane\",\"unit\":\"#21\",\"description\":\"Bright YOGA, studio next to transit.\",\"num_bedrooms\":1},{\"id\":\"6\",\"agent\":\"Mina Park\",\"unit\":\"#22\",\"description\":\"A DANCE/1-BEDROOM rehearsal room is listed nearby.\",\"num_bedrooms\":0},{\"id\":\"7\",\"agent\":\"Noah Reed\",\"unit\":\"#23\",\"description\":\"Quiet apartment near the river.\",\"num_bedrooms\":1}]"
return = [1, 0, 1]
FastPrep practice example: YOGA, studio and DANCE/1-BEDROOM are ignored because punctuation does not break the immediately preceding-word relationship. The last listing has no target word, so its original value remains 1.
Constraints
Input/Output[execution time limit] 4 seconds (py3)
[memory limit] 1 GB
[input] string jsonDataString in JSON format. It's guaranteed that each listing contains the fields "id", "agent", "unit", "description" and "num_bedrooms".
Guaranteed constraints: 136 ≤ jsonData.length ≤ 15366.
[output] array.integerReturn an array that contains the correct values for num_bedrooms for all of the listings in jsonData.
FastPrep practice clarification: jsonData is valid standard JSON; description is a string, num_bedrooms is 0 or 1, and each source target or modifier is matched as a complete ASCII word.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to tokenize each description into lowercase words, then scan them in order. Split on anything that isn't a letter, digit, or hyphen, so 1-bedroom stays one token and ART...studio gives you art then studio. For each token equal to studio or 1-bedroom, check the previous token. If it's yoga, dance, or art, skip it. Otherwise that token sets the answer: studio gives 0, 1-bedroom gives 1. If nothing valid is found, keep the original num_bedrooms. The pitfall is substring matching. Calling contains("studio") flags the yoga studio, and punctuation or casing breaks naive splits on spaces. Also parse the JSON string first, since the input is a string, not an array. Decide up front what happens when a listing has two valid matches. The statement says it won't mix both, so the first valid hit is fine. If you freeze on the regex or the previous-word check during the live OA, StealthCoder is the hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Correct Apartment Bedroom Counts cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Ramp's OA.
Ramp reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Correct Apartment Bedroom Counts FAQ
What's the trick in the Ramp apartment bedroom counts problem?+
Tokenize the description into lowercase words and check the word right before each studio or 1-bedroom token. If that previous word is yoga, dance, or art, ignore the match. Plain substring search fails because of the yoga studio and art studio examples.
How hard is this problem really?+
Easy on algorithm, annoying on details. There's no clever data structure. Points are lost on punctuation, casing, the hyphenated 1-bedroom token, and parsing the JSON string. Careful tokenizing gets you most of the way, so it's a precision test more than a difficulty test.
How should I handle punctuation and casing?+
Lowercase the whole description, then split on any character that isn't a letter, digit, or hyphen. That way ART...studio becomes art and studio as adjacent words. Keep the hyphen so 1-bedroom survives as a single token and matches exactly.
What if no valid studio or 1-bedroom appears?+
Return the listing's original num_bedrooms unchanged. This covers descriptions with no keyword and descriptions where every keyword is preceded by yoga, dance, or art. Build this fallback in first, then override it only when a valid token is found.
How do I prepare for this in 48 hours?+
Write a small tokenizer and JSON parse from scratch, then test it on the four sample listings, expecting [0, 1, 1, 0]. Add cases with caps, punctuation between words, and a keyword as the first word. Practice the previous-token check until it's automatic.