Questions based on higher-order functions — Part II

Janvi Arora
2 min readMay 1, 2024

--

Question 6: Suppose you have an array of car objects, each containing a make, model, and year property. How would you filter out cars manufactured after 2015 using higher-order functions?

Answer:

struct Car {
let make: String
let model: String
let year: Int
}

let cars = [
Car(make: "Toyota", model: "Camry", year: 2010),
Car(make: "Honda", model: "Accord", year: 2018),
Car(make: "Ford", model: "Mustang", year: 2012)
]

let recentCars = cars.filter { $0.year > 2015 }
print(recentCars)

Question 7: You have an array of movie objects, each containing a title, director, and rating property. How would you calculate the average rating for all movies directed by Christopher Nolan using higher-order functions?

struct Movie {
let title: String
let director: String
let rating: Double
}

let movies = [
Movie(title: "Inception", director: "Christopher Nolan", rating: 8.8),
Movie(title: "Interstellar", director: "Christopher Nolan", rating: 8.6),
Movie(title: "The Dark Knight", director: "Christopher Nolan", rating: 9.0)
]

let nolanMovies = movies.filter { $0.director == "Christopher Nolan" }
let averageRating = nolanMovies.reduce(0) { $0 + $1.rating } / Double(nolanMovies.count)
print("Average rating of Christopher Nolan's movies: \(averageRating)")

Question 8: Given an array of student objects, each containing a name and a list of grades, how would you find the student with the highest average grade using higher-order functions?

struct Student {
let name: String
let grades: [Double]
}

let students = [
Student(name: "Alice", grades: [85, 90, 88]),
Student(name: "Bob", grades: [75, 80, 82]),
Student(name: "Charlie", grades: [95, 92, 88])
]

if let topStudent = students.max(by: { (student1, student2) in
let average1 = student1.grades.reduce(0, +) / Double(student1.grades.count)
let average2 = student2.grades.reduce(0, +) / Double(student2.grades.count)
return average1 < average2
}) {
print("The top student is \(topStudent.name)")
} else {
print("No students found")
}

Question 9: Suppose you have an array of transaction objects, each containing a type (debit or credit) and an amount property. How would you calculate the total debit and credit amounts separately using higher-order functions?

enum TransactionType {
case debit
case credit
}

struct Transaction {
let type: TransactionType
let amount: Double
}

let transactions = [
Transaction(type: .debit, amount: 100),
Transaction(type: .credit, amount: 50),
Transaction(type: .debit, amount: 200),
Transaction(type: .credit, amount: 75)
]

let debitTotal = transactions.filter { $0.type == .debit }.map { $0.amount }.reduce(0, +)
let creditTotal = transactions.filter { $0.type == .credit }.map { $0.amount }.reduce(0, +)

print("Total debit amount: \(debitTotal)")
print("Total credit amount: \(creditTotal)")

Question 10: Given an array of email objects, each containing a sender, recipient, and timestamp property, how would you filter out emails sent by a specific sender within the last 24 hours using higher-order functions?

struct Email {
let sender: String
let recipient: String
let timestamp: Date
}

let emails = [
Email(sender: "sender1@example.com", recipient: "recipient@example.com", timestamp: Date(timeIntervalSinceNow: -86400)), // 1 day ago
Email(sender: "sender2@example.com", recipient: "recipient@example.com", timestamp: Date(timeIntervalSinceNow: -3600)), // 1 hour ago
Email(sender: "sender1@example.com", recipient: "recipient@example.com", timestamp: Date()) // Now
]

let senderToFilter = "sender1@example.com"
let filteredEmails = emails.filter { $0.sender == senderToFilter && $0.timestamp > Date(timeIntervalSinceNow: -86400) }
print(filteredEmails)

--

--

No responses yet