Real-Time Python Interview Experience - Count Duplicate Values from a List
Today I’m going to share a real-time interview experience that really improved my logical thinking as a DevOps engineer. During the interview, I was given a small Python task that looked simple at first, but it was actually a good brainstorming challenge.
The task was to convert a list into a dictionary and count how many times each car name appeared.
Interview Task
cars = ["audi", "audi", "audi", "bmw", "Jaguar", "jaguar"]The expected output was:
{'audi': 3, 'bmw': 1, 'jaguar': 2}At first, this task was a little confusing for me because I needed to understand how counting logic works in Python dictionaries. But after practicing the logic step by step, I solved it successfully.
Step-by-Step Solution
cars = ["audi", "audi", "audi", "bmw", "Jaguar", "jaguar"]final_car_count = {}for car in cars:
# Convert all values into lowercase
car = car.lower()
# Check if value already exists
if car not in final_car_count:
# First occurrence
final_car_count.update({car: 1})
else:
# Increase previous count
final_car_count.update({car: final_car_count[car] + 1})
print(final_car_count)
Output
{'audi': 3, 'bmw': 1, 'jaguar': 2}Easy Understanding of the Logic
- First, we created an empty dictionary.
- Then we used a
forloop to check every car name from the list. lower()is used to avoid case sensitivity issues like"Jaguar"and"jaguar".- If the car name is not already in the dictionary, we add it with value
1. - If the car name already exists, we increase its count by
1.
This type of question is very common in Python and DevOps interviews because it checks your:
- Logical thinking
- Problem-solving skills
- Understanding of loops and dictionaries
Final Thoughts
This was a simple but very tricky interview task. Small tasks like this help improve programming logic and confidence during technical interviews.
If you are preparing for DevOps or Python interviews, I highly recommend practicing these types of dictionary and loop-based problems daily.
I hope you liked this blog and learned something useful from this real-time interview experience.
🌐 Online References
- LinkedIn: https://www.linkedin.com/in/raees-yaqoob-qazi-ryqs/
- Medium Blog: https://medium.com/@raeesyaqubqazi
- Blogspot: https://brillertechnologies.blogspot.com/
- Facebook Page: https://web.facebook.com/profile.php?id=61553548371216
- YouTube Channel: https://www.youtube.com/@RaeesQ.
- TikTok: https://www.tiktok.com/@mrryqs?_t=ZS-8y7t0fQfJKu&_r=1
Comments
Post a Comment