WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Intermediate · question 26 of 100

What is the difference between groupBy, partition, and span operations in Scala collections?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

‘groupBy‘, ‘partition‘, and ‘span‘ are higher-order functions available for Scala collections. They are used to manipulate and transform collections based on a specified condition or logic. Let’s see the differences between these operations:

1. ‘groupBy‘:

‘groupBy‘ is a method available for Scala collections that accepts a function as its argument. This function is used to classify the elements of the collection. It returns a ‘Map‘ where the keys are the result of applying the function to the collection’s elements and the values are the subsequences of the elements that produce the respective keys.

The general signature of ‘groupBy‘ is:

def groupBy[K](f: (A) => K): Map[K, CC[A]]

Here, ‘A‘ is the type of the elements, ‘K‘ is the type of the keys, and ‘CC[A]‘ is the type of the result collection.

Example:

val numbers = List(1, 2, 3, 4, 5, 6)
val groups = numbers.groupBy(_ % 2)

println(groups) //Map(1 -> List(1, 3, 5), 0 -> List(2, 4, 6))

In this example, numbers are grouped by their remainder when divided by 2. The result is a ‘Map‘ where the keys are the remainders 0 and 1, and the values are the subsequences of the numbers that produce these remainders.

2. ‘partition‘:

‘partition‘ is a method available for Scala collections that also accepts a function as its argument. It is a more specialized version of ‘groupBy‘. The function defines a predicate for the elements of the collection. It returns a tuple of two collections: the first collection contains the elements for which the predicate holds true, and the second collection contains the elements for which the predicate is false.

The general signature of ‘partition‘ is:

def partition(p: (A) => Boolean): (CC[A], CC[A])

Example:

val numbers = List(1, 2, 3, 4, 5, 6)
val (evens, odds) = numbers.partition(_ % 2 == 0)

println(evens) //List(2, 4, 6)
println(odds) //List(1, 3, 5)

In this example, numbers are partitioned based on whether they are even or odd. The result is a tuple of two ‘List‘s: one containing even numbers and the other containing odd numbers.

3. ‘span‘:

‘span‘ is another method available for Scala collections, and it also accepts a function as its argument. However, it is different from ‘groupBy‘ and ‘partition‘. The function defines a predicate for the collection’s elements. ‘span‘ returns a tuple of two collections: the first collection contains the longest prefix of elements that satisfy the predicate, and the second collection contains the remaining elements.

The general signature of ‘span‘ is:

def span(p: (A) => Boolean): (CC[A], CC[A])

Example:

val numbers = List(1, 3, 5, 2, 4, 6)
val (odds, others) = numbers.span(_ % 2 != 0)

println(odds) //List(1, 3, 5)
println(others) //List(2, 4, 6)

In this example, numbers are spanned based on whether they are odd. The result is a tuple of two ‘List‘s: one containing the longest prefix of odd numbers and the other containing the remaining elements.

In summary:

- ‘groupBy‘ returns a ‘Map‘ after classifying elements based on the provided function.

- ‘partition‘ returns a tuple of two collections based on whether elements satisfy a given predicate.

- ‘span‘ returns a tuple of two collections corresponding to the longest prefix of elements that satisfy a given predicate and the remaining elements.

These operations offer different approaches to manipulating and transforming Scala collections based on specific conditions, predicates, and criteria.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics