Bird
Raised Fist0
Hadoopdata~20 mins

Reduce phase explained in Hadoop - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reduce Phase Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main role of the Reduce phase in Hadoop MapReduce?

In Hadoop MapReduce, after the Map phase processes input data, the Reduce phase takes over. What is the main role of the Reduce phase?

AIt sorts the input data before mapping.
BIt splits the input data into smaller chunks for processing.
CIt aggregates and summarizes the intermediate data produced by the Map phase.
DIt reads raw input data from the source files.
Attempts:
2 left
💡 Hint

Think about what happens after the Map phase outputs key-value pairs.

❓ Predict Output
intermediate
1:30remaining
Output of Reduce function aggregation

Consider the following simplified Reduce function in Hadoop MapReduce that sums values for each key:

def reduce(key, values):
    total = 0
    for v in values:
        total += v
    print(f"{key}: {total}")

What will be the output if the input to reduce is key = 'apple' and values = [2, 3, 5]?

Hadoop
def reduce(key, values):
    total = 0
    for v in values:
        total += v
    print(f"{key}: {total}")

reduce('apple', [2, 3, 5])
Aapple: 10
Bapple: 235
Capple: 0
Dapple: 5
Attempts:
2 left
💡 Hint

Sum all numbers in the list.

❓ data_output
advanced
2:00remaining
Resulting key-value pairs after Reduce phase

Given the following intermediate key-value pairs from the Map phase:

{'cat': [1, 1, 1], 'dog': [1, 1], 'bird': [1]}

If the Reduce phase sums the values for each key, what is the resulting output?

A{'cat': 1, 'dog': 1, 'bird': 1}
B{'cat': 3, 'dog': 2, 'bird': 1}
C{'cat': [3], 'dog': [2], 'bird': [1]}
D{'cat': 6, 'dog': 4, 'bird': 2}
Attempts:
2 left
💡 Hint

Sum the list of values for each key.

🔧 Debug
advanced
1:30remaining
Identify the error in this Reduce function

Look at this Reduce function code snippet:

def reduce(key, values):
    total = 0
    for v in values
        total += v
    print(f"{key}: {total}")

What error will this code produce when run?

ANameError because total is not defined
BIndentationError due to wrong indentation
CTypeError because values is not iterable
DSyntaxError due to missing colon after for loop
Attempts:
2 left
💡 Hint

Check the for loop syntax carefully.

🚀 Application
expert
2:00remaining
Choosing the correct Reduce phase output for word count

In a word count MapReduce job, the Map phase outputs key-value pairs where the key is a word and the value is 1 for each occurrence. The Reduce phase sums these counts. Given the following Map output for the word 'data':

[('data', 1), ('data', 1), ('data', 1), ('data', 1)]

Which of the following is the correct Reduce phase output for the key 'data'?

A('data', 4)
B('data', [1, 1, 1, 1])
C('data', 1)
D('data', 0)
Attempts:
2 left
💡 Hint

Sum all the counts for the word.

Practice

(1/5)
1.

What is the main purpose of the Reduce phase in Hadoop MapReduce?

easy
A. To combine and summarize data from the Map outputs by key
B. To split the input data into smaller chunks
C. To read data from the file system
D. To sort the input data before mapping

Solution

  1. Step 1: Understand the role of Reduce phase

    The Reduce phase takes the output from the Map phase and groups data by keys.
  2. Step 2: Identify the main function of Reduce

    It combines or aggregates the values for each key to produce summarized results.
  3. Final Answer:

    To combine and summarize data from the Map outputs by key -> Option A
  4. Quick Check:

    Reduce phase = combine by key [OK]
Hint: Reduce phase aggregates data by keys from Map outputs [OK]
Common Mistakes:
  • Confusing Reduce with Map phase
  • Thinking Reduce splits data
  • Assuming Reduce reads input files
2.

Which of the following is the correct signature of a Reduce function in Hadoop MapReduce?

public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException
easy
A. public void reduce(KEY key, VALUE value, Context context) throws IOException, InterruptedException
B. public void map(KEY key, VALUE value, Context context) throws IOException, InterruptedException
C. public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException
D. public void map(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException

Solution

  1. Step 1: Recall Reduce function signature

    The Reduce function receives a key and an iterable list of values for that key.
  2. Step 2: Match the correct method signature

    public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException correctly shows the reduce method with key, Iterable values, and context parameters.
  3. Final Answer:

    public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException -> Option C
  4. Quick Check:

    Reduce signature = key + Iterable values + context [OK]
Hint: Reduce gets key and list of values, not single value [OK]
Common Mistakes:
  • Confusing map and reduce method signatures
  • Using single VALUE instead of Iterable<VALUE> in reduce
  • Mixing map parameters with reduce function
3.

Given the following Reduce function snippet, what will be the output for key = "apple" and values = [2, 3, 5]?

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
    int sum = 0;
    for (IntWritable val : values) {
        sum += val.get();
    }
    context.write(key, new IntWritable(sum));
}
medium
A. (apple, 2)
B. (apple, 10)
C. (apple, 0)
D. (apple, 5)

Solution

  1. Step 1: Sum the values list

    Values are 2, 3, and 5. Summing gives 2 + 3 + 5 = 10.
  2. Step 2: Output key with summed value

    The context writes the key "apple" with the sum 10 as IntWritable.
  3. Final Answer:

    (apple, 10) -> Option B
  4. Quick Check:

    Sum of values = 10 [OK]
Hint: Add all values for key to get output [OK]
Common Mistakes:
  • Returning only first value instead of sum
  • Returning count instead of sum
  • Ignoring values and outputting zero
4.

Identify the error in this Reduce function code snippet:

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
    int sum = 0;
    for (IntWritable val : values) {
        sum = val.get();
    }
    context.write(key, new IntWritable(sum));
}
medium
A. The Iterable<IntWritable> should be a List<IntWritable>
B. The key parameter type is incorrect
C. The context.write method is missing
D. The sum variable is overwritten instead of accumulated

Solution

  1. Step 1: Analyze the for loop logic

    The code assigns sum = val.get() inside the loop, overwriting sum each time.
  2. Step 2: Identify correct accumulation

    Sum should accumulate values using sum += val.get() to add all values.
  3. Final Answer:

    The sum variable is overwritten instead of accumulated -> Option D
  4. Quick Check:

    Use += to accumulate sum, not = [OK]
Hint: Use += to add values, not = which overwrites [OK]
Common Mistakes:
  • Using = instead of += in accumulation
  • Confusing key type with error
  • Thinking context.write is missing
5.

You want to count the total occurrences of each word in a large text dataset using Hadoop MapReduce. Which of the following best describes what the Reduce phase should do?

hard
A. Receive each word as key and list of counts as values, then sum counts to get total occurrences
B. Split the input text into words and assign counts
C. Sort all words alphabetically before counting
D. Write the raw text data to output without changes

Solution

  1. Step 1: Understand MapReduce word count flow

    Map phase outputs (word, 1) pairs for each word occurrence.
  2. Step 2: Role of Reduce phase in counting

    Reduce receives each word as key and list of counts (1s), sums them to get total count.
  3. Final Answer:

    Receive each word as key and list of counts as values, then sum counts to get total occurrences -> Option A
  4. Quick Check:

    Reduce sums counts per word [OK]
Hint: Reduce sums counts for each word key [OK]
Common Mistakes:
  • Confusing Map and Reduce roles
  • Thinking Reduce sorts data
  • Assuming Reduce outputs raw data
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