Python Advanced Scanerio Based Question
Python Advanced Scanerio Based Question
Imagine you're working for a company that wants to calculate employee bonuses based on their
performance and years of service. The company has the following rules:
4.
If the performance score is below 80, they get no bonus.
You're given a list of employee data in the form of dictionaries, and your task is to write a Python
function to calculate the bonuses and return a report.
Example Input:
python
WrapCopy
employees = [
{"name": "Alice", "salary": 60000, "performance_score": 85,
"years_of_service": 3},
{"name": "Bob", "salary": 75000, "performance_score": 92,
"years_of_service": 7},
{"name": "Charlie", "salary": 50000, "performance_score": 78,
"years_of_service": 12},
{"name": "Diana", "salary": 80000, "performance_score": 95,
"years_of_service": 15}
]
Expected Output:
A list of dictionaries showing each employee's name, bonus amount, and a message explaining
the result.
Solution
python
WrapCopy
def calculate_bonus(employees):
report = []
return report
# Test data
employees = [
{"name": "Alice", "salary": 60000, "performance_score": 85,
"years_of_service": 3},
{"name": "Bob", "salary": 75000, "performance_score": 92,
"years_of_service": 7},
{"name": "Charlie", "salary": 50000, "performance_score": 78,
"years_of_service": 12},
{"name": "Diana", "salary": 80000, "performance_score": 95,
"years_of_service": 15}
]
Output
text
WrapCopy
1. Problem-Solving: It shows you can break down a real-world problem into logical steps.
2.
Python Basics: Uses dictionaries, conditionals (if-elif-else), loops, and string formatting.
3.
Clarity: The code is readable and well-structured, which interviewers value.
4.
Edge Cases: You can discuss how to handle edge cases (e.g., negative salaries,
missing data) if asked.
5.
Scalability: You can extend this by adding features like input validation or sorting the
report.
● Explain Your Approach: Start by walking through the problem (e.g., "First, I’ll check the
performance score, then calculate the bonus based on years of service").
●
Code Step-by-Step: Write the solution incrementally while explaining your thought
process.
●
Discuss Improvements: Mention how you could add error handling (e.g., try-except for
invalid inputs) or optimize it for larger datasets.
4.
If the performance score is below 80, they get no bonus.
You're given a list of employee data in the form of dictionaries, and your task is to write a Python
function to calculate the bonuses and return a report.
Example Input:
python
WrapCopy
employees = [
{"name": "Alice", "salary": 60000, "performance_score": 85,
"years_of_service": 3},
{"name": "Bob", "salary": 75000, "performance_score": 92,
"years_of_service": 7},
{"name": "Charlie", "salary": 50000, "performance_score": 78,
"years_of_service": 12},
{"name": "Diana", "salary": 80000, "performance_score": 95,
"years_of_service": 15}
]
Expected Output:
A list of dictionaries showing each employee's name, bonus amount, and a message explaining
the result.
Solution
python
WrapCopy
def calculate_bonus(employees):
report = []
# Calculate bonus
bonus = salary * bonus_percentage
message = f"{name} gets a bonus of ${bonus:.2f} based on {years}
years of service."
else:
bonus = 0
message = f"{name} is not eligible for a bonus due to a
performance score of {score}."
return report
# Test data
employees = [
{"name": "Alice", "salary": 60000, "performance_score": 85,
"years_of_service": 3},
{"name": "Bob", "salary": 75000, "performance_score": 92,
"years_of_service": 7},
{"name": "Charlie", "salary": 50000, "performance_score": 78,
"years_of_service": 12},
{"name": "Diana", "salary": 80000, "performance_score": 95,
"years_of_service": 15}
]
Output
text
WrapCopy
1. Problem-Solving: It shows you can break down a real-world problem into logical steps.
2.
Python Basics: Uses dictionaries, conditionals (if-elif-else), loops, and string formatting.
3.
Clarity: The code is readable and well-structured, which interviewers value.
4.
Edge Cases: You can discuss how to handle edge cases (e.g., negative salaries,
missing data) if asked.
5.
Scalability: You can extend this by adding features like input validation or sorting the
report.
● Explain Your Approach: Start by walking through the problem (e.g., "First, I’ll check the
performance score, then calculate the bonus based on years of service").
●
Code Step-by-Step: Write the solution incrementally while explaining your thought
process.
●
Discuss Improvements: Mention how you could add error handling (e.g., try-except for
invalid inputs) or optimize it for larger datasets.
Problem
You're tasked with analyzing customer feedback for a product. The company wants to know the
most frequently used words in a list of feedback comments, excluding common words like "the,"
"and," or "is." Write a Python function that takes a list of feedback strings and a list of words to
ignore, then returns a dictionary with the top 3 most frequent words and their counts.
Example Input:
python
WrapCopy
feedback = [
"The app is fast and user-friendly",
"Fast loading and great design",
"The design is sleek but slow at times",
"User-friendly app with fast performance"
]
ignore_words = ["the", "is", "and", "at", "with"]
Expected Output:
A dictionary like:
python
WrapCopy
{
"fast": 3,
"user-friendly": 2,
"design": 2
}
Solution:
python
WrapCopy
# Test data
feedback = [
"The app is fast and user-friendly",
"Fast loading and great design",
"The design is sleek but slow at times",
"User-friendly app with fast performance"
]
ignore_words = ["the", "is", "and", "at", "with"]
Output:
text
WrapCopy
● Skills Demonstrated: String manipulation, dictionary usage, sorting with custom keys
(lambda), and filtering.
●
Discussion Points: You could talk about handling edge cases (e.g., empty feedback),
optimizing for large datasets (e.g., using collections.Counter), or adding regex for better
text cleaning.
●
Relevance: Text analysis is common in data processing or NLP tasks.
Problem
Imagine you're building a tool to analyze disk usage. You're given a nested directory structure
represented as a dictionary, where each key is a folder or file name, and values are either
nested dictionaries (for folders) or integers (file sizes in KB). Write a recursive Python function to
calculate the total size of the directory.
Example Input:
python
WrapCopy
directory = {
"root": {
"folder1": {
"file1": 100,
"file2": 200
},
"folder2": {
"file3": 300
},
"file4": 50
}
Expected Output:
text
WrapCopy
Solution:
python
WrapCopy
def calculate_directory_size(directory):
total_size = 0
return total_size
# Test data
directory = {
"root": {
"folder1": {
"file1": 100,
"file2": 200
},
"folder2": {
"file3": 300
},
"file4": 50
}
Output:
text
WrapCopy
Interview Value:
● Skills Demonstrated: Recursion, type checking (isinstance), and working with nested
data structures.
●
Discussion Points: You could discuss handling invalid inputs (e.g., negative sizes),
adding error handling, or optimizing for performance with memoization if asked.
●
Relevance: Recursive problems are a staple in coding interviews, and this mimics
real-world file system traversal.