Remove Comments - Problem
Remove Comments from C++ Source Code

Imagine you're building a code preprocessor that needs to clean up C++ source code by removing all comments before compilation. You're given a C++ program as an array of strings, where each string represents a line of source code.

Your task is to remove two types of comments:

๐Ÿ”ธ Line comments (//): Everything from // to the end of that line should be removed
๐Ÿ”ธ Block comments (/* ... */): Everything between /* and */ should be removed, even across multiple lines

Important rules:
โ€ข Comments inside other comments are ignored (e.g., // inside a block comment)
โ€ข Empty lines after removing comments should be excluded from output
โ€ข Block comments can span multiple lines and effectively "delete" newlines
โ€ข The string /**/ does NOT end a block comment (overlapping issue)

Goal: Return the cleaned source code with all comments removed, maintaining the same array format but excluding empty lines.

Example: ["/*Test program */", "int main(){", " // variable declaration", " int a = 1;", " /* comment */", " return 0;", "}"] becomes ["int main(){", " int a = 1;", " return 0;", "}"]

Input & Output

basic_comments.py โ€” Python
$ Input: source = ["/*Test program */", "int main(){", " // variable declaration", " int a = 1;", " /* comment */", " return 0;", "}"]
โ€บ Output: ["int main(){", " int a = 1;", " return 0;", "}"]
๐Ÿ’ก Note: The first line is entirely a block comment, so it's removed. The line comment removes everything after //, and the single-line block comment is removed while preserving the rest of the line (which becomes just whitespace and is excluded).
multiline_block.py โ€” Python
$ Input: source = ["a/*comment", "line", "more_comment*/b"]
โ€บ Output: ["ab"]
๐Ÿ’ก Note: The block comment spans multiple lines, effectively removing the newline characters. The result concatenates 'a' from the first line with 'b' from the third line into a single line 'ab'.
nested_comments.py โ€” Python
$ Input: source = ["struct Node{", " /*// Constructor */", " int val;", " /* // Another comment", " with multiple lines */", " int next;", "};"]
โ€บ Output: ["struct Node{", " int val;", " int next;", "};"]
๐Ÿ’ก Note: Line comments inside block comments are ignored. The '//' inside the block comments don't start new line comments because they're already inside block comments.

Constraints

  • 1 โ‰ค source.length โ‰ค 100
  • 0 โ‰ค source[i].length โ‰ค 80
  • source[i] consists of printable ASCII characters
  • Every open block comment will eventually be closed
  • There are no single quotes, double quotes, or control characters in the source code

Visualization

Tap to expand
Comment Removal Process VisualizationInput: int main() { /* block comment */ return 0; // line comment }NormalLineBlock///**/newlineCharacter Processing:โœ“ Collect: i n t m a i n ( ) { }โœ— Skip: /* block comment */โœ“ Collect: r e t u r n 0 ;โœ— Skip: // line comment }Output: "int main() { return 0;"
Understanding the Visualization
1
Start in Normal State
Begin processing characters in normal code mode
2
Detect Comment Patterns
Look for '//' or '/*' patterns while in normal state
3
Enter Comment State
Switch to appropriate comment state and skip characters
4
Exit Comment State
Return to normal state when comment ends (newline for //, */ for block)
5
Build Result
Collect non-comment characters and form cleaned source code
Key Takeaway
๐ŸŽฏ Key Insight: Use a finite state machine to elegantly handle the different modes of text processing, ensuring proper comment removal while preserving code structure.
Asked in
Google 23 Microsoft 18 Amazon 15 Facebook 12
24.7K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen