Perfect — that’s exactly the depth we want. For each problem, I’ll now include:
🧠 Updated Problem-Solving Framework
- ✅ Problem Statement
- 📄 Pseudocode / Flow / Logic
- 🎯 Why this Pattern / Data Structure
- 🔁 Looping & Decision Points
- 🔽 Input → Logic → Output (Structured Code)
- 🧪 Dry Run / Output Example
Let’s go all-in with this new format! Below are 5 full examples in this format. Once you’re happy with this structure, I’ll send 10 more in batches.
✅ Example 1: Check if a String is a Palindrome
1. ✅ Problem Statement
Check if the given string reads the same backward as forward.
Example: "madam"
is a palindrome; "hello"
is not.
2. 📄 Pseudocode:
- Input a string
- Reverse the string
- Compare reversed with original
- If equal → Palindrome
3. 🎯 Why This Pattern / Data Structure?
- We use string slicing (
[::-1]
) — no need for additional memory - Efficient O(n) time, O(1) extra space
4. 🔁 Looping / Decisions
- No explicit loop, slicing handles reversal.
- One comparison:
s == s[::-1]
5. 🔽 Code (Input → Logic → Output):
def get_input():
return input("Enter a string: ")
def process(data):
return data == data[::-1]
def show_result(output):
print("Palindrome?", output)
def main():
data = get_input()
result = process(data)
show_result(result)
main()
6. 🧪 Dry Run:
Input: madam
data[::-1] = madam
madam == madam → True
✅ Example 2: Count Frequency of Characters
1. ✅ Problem Statement
Given a string, count the frequency of each character.
2. 📄 Pseudocode:
- Create empty dict
- For each char in string:
- If already in dict: increment
- Else: set count = 1
- Return dict
3. 🎯 Why This Pattern / DS?
- HashMap (dict) gives O(1) lookup/update for frequency
- Best for counting problems
4. 🔁 Looping / Decisions
- Loop over each character
if char in dict
decides update or initialize
5. 🔽 Code:
def get_input():
return input("Enter a word: ")
def process(word):
freq = {}
for ch in word:
freq[ch] = freq.get(ch, 0) + 1
return freq
def show_result(output):
print("Character Frequency:", output)
def main():
data = get_input()
result = process(data)
show_result(result)
main()
6. 🧪 Dry Run:
Input: apple
freq = {'a':1, 'p':2, 'l':1, 'e':1}
✅ Example 3: Find the Minimum in a List
1. ✅ Problem Statement
Given a list of numbers, find the minimum element.
2. 📄 Pseudocode:
- Initialize min_val = first element
- Loop from index 1 → n:
- if arr[i] < min_val → update min_val
- Return min_val
3. 🎯 Why This Pattern / DS?
- Simple iteration, no need for sorting
- Optimal in one pass → O(n)
4. 🔁 Looping / Decisions
- Loop: index 1 to end
- Compare each with current
min_val
5. 🔽 Code:
def get_input():
return list(map(int, input("Enter numbers separated by space: ").split()))
def process(arr):
min_val = arr[0]
for num in arr[1:]:
if num < min_val:
min_val = num
return min_val
def show_result(output):
print("Minimum value:", output)
def main():
data = get_input()
result = process(data)
show_result(result)
main()
6. 🧪 Dry Run:
Input: [4, 9, 1, 2]
min_val starts at 4 → becomes 1 → stays 1
✅ Example 4: Check if Two Strings are Anagrams
1. ✅ Problem Statement
An anagram has the same characters in different order.
Check if two strings are anagrams.
2. 📄 Pseudocode:
- Count characters in both strings using dict
- Compare both dicts
3. 🎯 Why This Pattern / DS?
- HashMap allows fast comparison
- O(n) for each string
4. 🔁 Looping / Decisions
- 2 loops for 2 words → 2 dicts
- 1 comparison → dict1 == dict2
5. 🔽 Code:
def get_input():
a = input("Enter first string: ")
b = input("Enter second string: ")
return a, b
def count_chars(word):
freq = {}
for ch in word:
freq[ch] = freq.get(ch, 0) + 1
return freq
def process(a, b):
return count_chars(a) == count_chars(b)
def show_result(output):
print("Anagram?", output)
def main():
a, b = get_input()
result = process(a, b)
show_result(result)
main()
6. 🧪 Dry Run:
a = "listen", b = "silent"
Both produce {'l':1,'i':1,'s':1,'t':1,'e':1,'n':1} → True
✅ Example 5: FizzBuzz (Classic Logic)
1. ✅ Problem Statement
Print numbers from 1 to n with these rules:
- If divisible by 3 → “Fizz”
- If divisible by 5 → “Buzz”
- If both → “FizzBuzz”
- Else → number
2. 📄 Pseudocode:
For i from 1 to n:
if i % 3 == 0 and i % 5 == 0 → FizzBuzz
elif i % 3 == 0 → Fizz
elif i % 5 == 0 → Buzz
else → i
3. 🎯 Why This Pattern?
- Simple modulo logic
- Sequence generation + condition branching
4. 🔁 Looping / Decision
- Loop from 1 to n
- Decision tree with 3 levels
5. 🔽 Code:
def get_input():
return int(input("Enter a number: "))
def process(n):
result = []
for i in range(1, n+1):
if i % 15 == 0:
result.append("FizzBuzz")
elif i % 3 == 0:
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(str(i))
return result
def show_result(output):
print("FizzBuzz Sequence:")
print(", ".join(output))
def main():
n = get_input()
result = process(n)
show_result(result)
main()
6. 🧪 Dry Run:
Input: 5 → Output: 1, 2, Fizz, 4, Buzz
✅ Ready for the Next 10+?
I’ll follow the exact same format for:
- Prime Number Check
- Reverse Words in Sentence
- Matrix Diagonal Sum
- First Non-Repeating Character
- Longest Unique Substring
- Valid Parentheses
- Count Words in String
- Roman to Integer
- Binary Search
- Bubble Sort
Leave a Reply