Bird
Raised Fist0
Swiftprogramming~20 mins

Equatable, Hashable, Comparable protocols in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Protocols Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Equatable conformance check
What is the output of this Swift code snippet?
Swift
struct Point: Equatable {
    var x: Int
    var y: Int
}

let p1 = Point(x: 3, y: 4)
let p2 = Point(x: 3, y: 4)
print(p1 == p2)
Atrue
Bfalse
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Structs with Equatable automatically get == if all properties are Equatable.
❓ Predict Output
intermediate
2:00remaining
Hash value consistency with Hashable
What will be printed by this Swift code?
Swift
struct User: Hashable {
    var id: Int
    var name: String
}

let user1 = User(id: 1, name: "Alice")
let user2 = User(id: 1, name: "Alice")
print(user1.hashValue == user2.hashValue)
ARuntime error
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Hashable structs get synthesized hashValue based on all properties.
❓ Predict Output
advanced
2:00remaining
Output of Comparable custom implementation
What is the output of this Swift code?
Swift
struct Box: Comparable {
    var volume: Int
    static func < (lhs: Box, rhs: Box) -> Bool {
        return lhs.volume > rhs.volume
    }
}

let b1 = Box(volume: 10)
let b2 = Box(volume: 20)
print(b1 < b2)
Atrue
Bfalse
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
The < operator is reversed in the implementation.
❓ Predict Output
advanced
2:00remaining
Result of sorting with Comparable conformance
What will be the output of this Swift code?
Swift
struct Person: Comparable {
    var age: Int
    static func < (lhs: Person, rhs: Person) -> Bool {
        return lhs.age < rhs.age
    }
}

let people = [Person(age: 30), Person(age: 20), Person(age: 40)]
let sortedAges = people.sorted().map { $0.age }
print(sortedAges)
A[30, 20, 40]
B[40, 30, 20]
C[20, 30, 40]
DCompilation error
Attempts:
2 left
💡 Hint
Sorting uses the < operator defined in Comparable.
🧠 Conceptual
expert
3:00remaining
Why implement Equatable and Hashable together?
Why is it important to implement both Equatable and Hashable protocols consistently for a Swift type used as a dictionary key?
ABecause dictionary keys require both equality checks and hash values to store and retrieve values correctly.
BBecause <code>Hashable</code> automatically provides <code>Equatable</code> conformance, so only one is needed.
CBecause <code>Equatable</code> is only for classes and <code>Hashable</code> is only for structs.
DBecause implementing both improves performance but is not required for dictionary keys.
Attempts:
2 left
💡 Hint
Think about how dictionaries find values by keys.

Practice

(1/5)
1.

Which protocol in Swift allows you to check if two instances of a type are equal using ==?

easy
A. Hashable
B. Codable
C. Comparable
D. Equatable

Solution

  1. Step 1: Understand the purpose of Equatable

    Equatable protocol allows comparing two instances for equality using ==.
  2. Step 2: Compare with other protocols

    Hashable is for hashing, Comparable is for ordering, Codable is for encoding/decoding.
  3. Final Answer:

    Equatable -> Option D
  4. Quick Check:

    Equality check = Equatable [OK]
Hint: Equality means Equatable protocol [OK]
Common Mistakes:
  • Confusing Hashable with Equatable
  • Thinking Comparable checks equality
  • Mixing Codable with Equatable
2.

Which of the following is the correct way to declare a struct Person that conforms to Equatable and Hashable?

struct Person: ??? {
    var name: String
    var age: Int
}
easy
A. Equatable, Hashable
B. Comparable, Hashable
C. Equatable, Comparable
D. Codable, Hashable

Solution

  1. Step 1: Identify needed protocols

    To compare equality and use in sets/dictionaries, use Equatable and Hashable.
  2. Step 2: Check syntax for protocol conformance

    Protocols are listed separated by commas after colon in struct declaration.
  3. Final Answer:

    Equatable, Hashable -> Option A
  4. Quick Check:

    Equality + hashing = Equatable, Hashable [OK]
Hint: List protocols comma-separated after colon [OK]
Common Mistakes:
  • Using Comparable instead of Equatable
  • Forgetting commas between protocols
  • Using Codable instead of Equatable
3.

What will be the output of the following Swift code?

struct Item: Comparable {
    var value: Int
    static func < (lhs: Item, rhs: Item) -> Bool {
        return lhs.value < rhs.value
    }
}

let items = [Item(value: 3), Item(value: 1), Item(value: 2)]
let sortedValues = items.sorted().map { $0.value }
print(sortedValues)
medium
A. [3, 2, 1]
B. [1, 2, 3]
C. [2, 3, 1]
D. Compilation error

Solution

  1. Step 1: Understand Comparable conformance

    The < operator compares value properties to sort ascending.
  2. Step 2: Analyze sorting and mapping

    Sorting the array orders items by value: 1, 2, 3. Mapping extracts values in this order.
  3. Final Answer:

    [1, 2, 3] -> Option B
  4. Quick Check:

    Sorted values ascending = [1, 2, 3] [OK]
Hint: Comparable sorts ascending by < operator [OK]
Common Mistakes:
  • Assuming descending order by default
  • Confusing map output with original array
  • Forgetting to implement < operator
4.

Identify the error in this Swift struct that tries to conform to Hashable:

struct Book: Hashable {
    var title: String
    var pages: Int

    func hash(into hasher: inout Hasher) {
        hasher.combine(title)
    }
}
medium
A. Missing combine call for all properties in hash(into:)
B. Struct cannot conform to Hashable without Codable
C. Incorrect method signature for hash(into:)
D. Missing == operator implementation for Equatable

Solution

  1. Step 1: Check Hashable requirements

    Hashable requires hashing all properties that affect equality.
  2. Step 2: Analyze hash(into:) implementation

    Only title is combined; pages is missing, causing incorrect hashing.
  3. Final Answer:

    Missing combine call for all properties in hash(into:) -> Option A
  4. Quick Check:

    Hash all relevant properties [OK]
Hint: Hash all properties used in equality [OK]
Common Mistakes:
  • Hashing only some properties
  • Forgetting to implement == operator if needed
  • Wrong method signature for hash(into:)
5.

You want to create a struct Player that can be stored in a Set, compared for equality, and sorted by score descending. Which protocols should Player conform to, and how should you implement the sorting?

hard
A. Conform to Equatable and Hashable; implement < to compare scores descending
B. Conform to Comparable only; implement < to compare scores ascending
C. Conform to Equatable, Hashable, Comparable; implement < to compare scores descending
D. Conform to Hashable only; sorting is automatic

Solution

  1. Step 1: Identify needed protocols

    To store in Set and check equality, conform to Equatable and Hashable. To sort, conform to Comparable.
  2. Step 2: Implement sorting logic

    Implement < operator to compare scores descending by reversing comparison.
  3. Final Answer:

    Conform to Equatable, Hashable, Comparable; implement < for descending scores -> Option C
  4. Quick Check:

    Set + equality + custom sort = Equatable, Hashable, Comparable [OK]
Hint: Use all three protocols; reverse < for descending sort [OK]
Common Mistakes:
  • Forgetting Comparable for sorting
  • Sorting ascending instead of descending
  • Missing Equatable or Hashable for Set storage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard