Try it out
Problem Arpasland has surrounded by attackers. A truck enters the city. The driver claims the load is food and medicine from Iranians. Ali is one of the soldiers in Arpasland. He doubts about the truck, maybe it's from the siege. He knows that a tag is valid if the sum of every two consecutive digits of it is even and its letter is not a vowel. Determine if the tag of the truck is valid or not. We consider the letters "A","E","I","O","U","Y" to be vowels for this problem. Input Format The first line contains a string of length 9. The format is "DDXDDD-DD", where D stands for a digit (non zero) and X is an uppercase english letter. Output Format Print "valid" (without quotes) if the tag is valid, print "invalid" otherwise (without quotes) Sample Input 12X345-67 Sample Output invalid Time Limit: 1 Memory Limit: 256 Source Limit: Explanation The tag is invalid because the sum of first and second digit of it is odd (also the sum of 4'th and 5'th, 5'th and 6'th and 8'th and 9'th are odd).Psuedo-code
There are two ways I can think of for solving this problem.Way 1
1. Assuming that the format of tag does not change over time. To determine if a tag is valid or not, I would take following steps: 2. I would check if tag[2] is a vowel or not. If it's a vowel: return invalid. 3. I would sum up tag[0]+tag[1] and check if it is even. If it's odd: return invalid. 4. I would sum up tag[4]+tag[5] and check if it is even. If it's odd: return invalid. 5. I would sum up tag[5]+tag[6] and check if it is even. If it's odd: return invalid. 6. I would sum up tag[8]+tag[9] and check if it is even. If it's odd: return invalid. 7. Return valid.Way 2
1. Assuming that the format of tag does change over time with only allowed characters as A-Z, 1-9 and "-". To determine if a tag is valid or not, I would take the following steps: 2. I would loop over all the characters of the tag. 3. Initialize a variable last_char as "NA". 3. For first character: if it's a number: store it in last_char. Else if is a alphabet: check if is a vowel, if it's vowel then return invalid. 4. Second character onwards: if it's a number and last_char is also a number: sum up the number and last_char. If the sum is odd, return invalid. Else if it's alphabet: check if is a vowel, if it's vowel then return invalid. 5. Return valid. Compare and contrast two approaches and critique both.
Critique by ChatGPT
Great job outlining two viable approaches to this problem! Both are valid and solve the problem in different ways. Let’s break them down one by one, compare them, and identify pros and cons to help you decide which is better in different situations.
✅ Restating the Problem Requirements
Given a string like "12X345-67"
, you must:
-
Check if the letter in position
2
is not a vowel. -
Check if the sum of specific consecutive digit pairs is even:
-
tag[0] + tag[1]
-
tag[3] + tag[4]
-
tag[4] + tag[5]
-
tag[7] + tag[8]
-
Now let's evaluate both your approaches.
✅ Way 1: Index-based validation (Fixed Format)
๐ Steps Recap:
-
Check
tag[2]
is not a vowel. -
Validate digit pairs:
[0+1], [3+4], [4+5], [7+8]
-
Return
invalid
if any check fails.
✅ Pros:
-
Highly efficient (constant time, O(1)) — only 4 fixed checks.
-
Simple and readable if the format is guaranteed.
-
Minimal computation — avoids unnecessary looping.
❌ Cons:
-
Rigid: Completely breaks if the format changes.
-
e.g., if the position of digits or letters changes, or new rules are added.
-
-
Not easily reusable for other patterns.
๐ Best use-case:
When the format is strictly fixed — great for tightly scoped system checks like license plate formats or barcode rules.
✅ Way 2: Character-based validation (Dynamic Format)
๐ Steps Recap:
-
Loop over the string.
-
Track the last digit seen.
-
For each character:
-
If alphabet → check if vowel.
-
If digit:
-
If previous was also digit → check if sum is even.
-
-
✅ Pros:
-
Flexible: Works even if the format evolves or changes.
-
Scalable: Easier to extend or modify (e.g., different lengths or more rules).
-
Robust: Does not rely on exact positions.
❌ Cons:
-
Less performant: O(n), though still fast for small input (n=9).
-
Slightly more complex logic-wise than hardcoded index checks.
-
Might validate unintended patterns if not carefully handled (e.g., two digits from separate segments).
๐ Best use-case:
When the input format can vary, or the rules are expected to change or be reused in other contexts.
⚖️ Comparison Summary Table
Criteria | Way 1 (Fixed Format) | Way 2 (Flexible Format) |
---|---|---|
Performance | ✅ Faster (O(1)) | ❌ Slower (O(n)) |
Flexibility | ❌ Not flexible | ✅ Highly flexible |
Readability | ✅ Easier to follow | ⚠ Slightly more complex |
Robustness | ❌ Fragile to format change | ✅ Handles varied inputs |
Scalability | ❌ Hard to extend | ✅ Easy to generalize |
Best for | Fixed formats | Variable formats |
๐ Conclusion
-
Use Way 1 if you're working in a controlled environment with fixed format tags (like this specific problem).
-
Use Way 2 if you want your code to be robust, extensible, or you’re designing for systems where formats may change (e.g., real-world parsers or validators).
If this were an interview, I'd recommend starting with Way 1 for efficiency and then explaining that Way 2 could be used in production systems for robustness.
Let me know if you'd like implementations of both approaches for comparison.
How would you solve it?
To solve the "Truck Tag Validator" problem, I would choose Way 1 (fixed index-based approach) because:
-
The format
"DDXDDD-DD"
is fixed (length is always 9). -
Index-based validation is clean, fast, and easy to reason about for such problems.
✅ Key Validation Rules (based on fixed positions)
Given the string: tag = "DDXDDD-DD"
, check:
-
The character at index 2 must not be a vowel:
'A', 'E', 'I', 'O', 'U', 'Y'
-
The sum of digits at positions:
-
0 + 1
must be even -
3 + 4
must be even -
4 + 5
must be even -
7 + 8
must be even
-
✅ Python Solution:
๐งช Sample Run:
For input: "12X345-67"
-
tag[2] = X
→ Not a vowel ✅ -
1+2 = 3
→ Odd ❌ → Return"invalid"
⚙️ Time & Space Complexity
-
Time: O(1) → constant number of checks
-
Space: O(1) → uses only a few variables
Let me know if you'd like the flexible (Way 2) version too or want to make it more generic.
No comments:
Post a Comment