Remove Comments

MediumStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a C-style program as a list of lines source. Strip out all comments and return the remaining lines. There are two kinds of comments. A **line comment** starts with // and removes itself and everything after it on that line. A **block comment** starts with /* and ends at the next */ that begins after the opening /* (so /*/ does not close itself); it may span several lines, and the newlines inside it are removed too, joining the text before it with the text after it into one line. Whichever of // or /* appears first takes effect; comment markers inside an active comment are ignored. After stripping, drop any line that is empty. Keep all other characters (including spaces) exactly as they are, and return the lines in their original order.

Examples

Input: source = ["int main() {", "// entry point", " int a = 1; /* start", " still comment */ return a;", "}"]
Output: ["int main() {"," int a = 1; return a;","}"]
Line 2 becomes empty and is dropped. The block comment spans lines 3 and 4, so the text around it joins into one line.
Input: source = ["a/*comment", "line", "more_comment*/b"]
Output: ["ab"]
Everything from `/*` to `*/` is removed, including the newlines, leaving `a` and `b` on one line.

Constraints

  • `1 <= source.length <= 100`
  • `0 <= source[i].length <= 80`
  • `source[i]` contains printable ASCII characters
  • Every block comment that is opened is eventually closed
  • There are no string or character literals

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems