Problem: Remove Consecutive Duplicates Given a string s, remove all consecutive duplicate characters and return the resulting string. Input Format A single line containing a string s (1 ≤ length of s ≤ 1000) The string will contain only lowercase English letters. Output Format Print the string after removing consecutive duplicate characters. Sample Input 1 aabbccddeee Sample Output 1 abcde Sample Input 2 aabaaabb Sample Output 2 abab
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Can you ask me a (that may be simple at rating of about 3/10) coding problem that tests problem solving skills of the candidate?
Sure! Here's a simple (3/10 difficulty) coding problem that tests basic problem-solving, string handling, and logic skills:
✅ Problem: Remove Consecutive Duplicates
Given a string s, remove all consecutive duplicate characters and return the resulting string.
ЁЯФ╣ Input Format
-
A single line containing a string
s(1 ≤ length of s ≤ 1000) -
The string will contain only lowercase English letters.
ЁЯФ╣ Output Format
-
Print the string after removing consecutive duplicate characters.
ЁЯФ╣ Sample Input 1
ЁЯФ╣ Sample Output 1
ЁЯФ╣ Sample Input 2
ЁЯФ╣ Sample Output 2
ЁЯза What it tests
-
Understanding of string traversal
-
Use of conditionals
-
Handling edge cases like repeated or single characters
Let me know when you're ready for the solution or hints!
Can you critique this rough algo:
s = "afafasgggewrtdg"
t = ""
last_seen_character = ''
streak_found = False
for c in s:
if c not in t:
t += c
last_seen_character = c
elif c in t and last_seen_character == c:
streak_found = True
last_seen_character = c
continue
else (c in t) and last_seen_character != c and not streak_found:
t += c
last_seen_character = c
streak_found = False
Thanks for sharing your rough algorithm! You're clearly thinking through the logic and edge cases, which is great. Let’s break down the approach and evaluate it step by step.
ЁЯФН Your Goal:
You want to remove consecutive duplicate characters from a string.
✅ What’s good in your approach:
-
Tracking state: You're maintaining
last_seen_character— that’s useful for detecting consecutive repeats. -
Use of flags: You introduced
streak_foundto handle streaks, though it's a bit more complex than needed for this problem. -
Good intent: You’re trying to avoid adding characters that have already been added if they’re part of a consecutive streak.
❌ Issues in the Logic:
-
Wrong check for character presence in
t:-
You’re checking
if c not in t, but this checks if the character has ever occurred int, not if it's the same as the previous one. -
This can cause issues with strings like
"aabaaabb"— you'll incorrectly skip valid characters.
-
-
Incorrect
elsecondition:-
Python syntax error:
else (c in t) and ...is invalid. -
elsedoesn’t take a condition. You’d need to useelifagain.
-
-
Unnecessary use of
streak_found:-
You don’t need a
streak_foundflag. You only need to compare each character to the one immediately before it.
-
-
Adding characters that are not duplicates but previously seen:
-
You're preventing characters from being added again even if they're not consecutive — which is incorrect.
-
✅ Correct Logic Should Be:
Keep the character only if it’s not equal to the previous character.
✔️ Cleaned Up Version of the Algorithm:
ЁЯзк Example:
Input: "aabaaabb"
Output: "abab"
⏱️ Time Complexity: O(n)
You visit each character once.
ЁЯУж Space Complexity: O(n)
In the worst case, the output string could be the same length as input.
ЁЯза Final Thoughts:
You're on the right track with tracking characters, but overcomplicating it with multiple conditions and flags. Stick to comparing only the current and previous characters — that’s sufficient here.
Let me know if you want to walk through more test cases or edge conditions!
So The Solution Is:
s = "afafasgggewrtdg"
t = ""
last_seen_character = None
for c in s:
if c != last_seen_character:
t += c
last_seen_character = c
print(t)

No comments:
Post a Comment