---
title: "Max and min word occurrence with MapReduce"
subject: "Big Data Technologies"
chapter: 3
key: "semester_8/big_data_technologies:mapreduce-max-min-word-occurrence"
source: https://www.bctengineer.com/q/semester_8/big_data_technologies/mapreduce-max-min-word-occurrence
site: BCT Engineering
---

# Max and min word occurrence with MapReduce

Big Data Technologies · Chapter 3: Map-Reduce Framework

- Asked in: 2073 Bhadra, 10 marks
- Video: [Map Reduce Example: WordCount and Weblink](https://www.youtube.com/watch?v=MOARe18YYr4) — Yan Luo
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_8/big_data_technologies/mapreduce-max-min-word-occurrence
- Whole chapter: https://www.bctengineer.com/md/semester_8/big_data_technologies/chapter-3

## Question

How do you find max and min occurrence of the words in a given text document? Explain.

## Short answer

First run a word count to get frequencies. Then in a second job, map each (word, count) to a common key and reduce to find global max and min, with mappers sending only local extremes to avoid bottlenecks. For text 'to be or not to be', max count is 2 (be, to) and min is 1 (not, or).

## Answer

To find the most and least frequent words, first run a **word count** job to get the number of occurrences of each word. Then find the maximum and minimum of those counts. This is done either with a second MapReduce job, or with a single job whose reducers keep track of the extremes.

#### Job 1: Word count

```text
map(offset, line):
    for each word w in line:
        emit(w.lower(), 1)

reduce(word, counts):
    emit(word, sum(counts))
```

A combiner (same as the reducer) can be used to cut network traffic. Output example for text "to be or not to be": `(be,2) (not,1) (or,1) (to,2)`.

#### Job 2: Find the global max and min

Every word-count pair is sent to one common key so that a single reducer sees all counts.

```text
map(word, count):
    emit("ALL", (word, count))

reduce("ALL", pairs):
    maxW = pair with highest count
    minW = pair with lowest count
    emit("MAX", maxW)
    emit("MIN", minW)
```

Because all data goes to one reducer, each mapper should send only its local max and min pairs (just two records), using a combiner or in-mapper combining as below, so the single reducer does not become a bottleneck.

```java
// Mapper2 keeps local extremes in memory, emits in cleanup()
private int maxCnt = Integer.MIN_VALUE, minCnt = Integer.MAX_VALUE;
private String maxWord = "", minWord = "";

public void map(Text word, IntWritable cnt, Context c) {
  if (cnt.get() > maxCnt) { maxCnt = cnt.get(); maxWord = word.toString(); }
  if (cnt.get() < minCnt) { minCnt = cnt.get(); minWord = word.toString(); }
}
protected void cleanup(Context c) throws IOException, InterruptedException {
  c.write(new Text("ALL"), new Text(maxWord + "," + maxCnt));
  c.write(new Text("ALL"), new Text(minWord + "," + minCnt));
}
```

The reducer then compares the few local extremes from all mappers and writes the global maximum and minimum.

#### Example

Text: "to be or not to be" gives counts `be=2, not=1, or=1, to=2`. Max count = 2 (words `be` and `to`), min count = 1 (words `not` and `or`). In case of ties, the reducer can emit all tied words.

#### Data flow

```text
 Text -> [Job1: map/reduce] -> (word,count)
      -> [Job2: map -> ALL -> reduce] -> MAX, MIN
```

Alternatively, with a single reducer in Job 1 (`setNumReduceTasks(1)`), the reducer can track the running max and min and emit them in `cleanup()`. This is simple but does not scale as well.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the first step to find most and least frequent words?
   A. Sort the document alphabetically
   B. Run a word count job
   C. Find the longest word
   D. Use a combiner only
   Answer: B. Counts per word are needed before any max or min can be found. (Key point: First step)

2. Why does Job 2 emit every pair under one common key like 'ALL'?
   A. To make a single reducer see all counts
   B. To sort words
   C. To delete duplicates
   D. To save HDFS space
   Answer: A. A global max/min needs one reducer to compare all counts. (Key point: Single key)

3. Why emit only local max and min from each mapper?
   A. Counts are wrong otherwise
   B. It is required by HDFS
   C. It avoids the single reducer becoming a bottleneck
   D. It removes ties
   Answer: C. Each mapper sends just two records, so the lone reducer receives little data. (Key point: Local extremes)

4. For 'to be or not to be', what are max and min counts?
   A. Max 2 (be, to); min 1 (not, or)
   B. Max 2 (to only); min 1 (or only)
   C. Max 1; min 1
   D. Max 3; min 1
   Answer: A. be=2, to=2, not=1, or=1, with ties reported for both. (Key point: Ties)

## Related in Big Data Technologies

- [MapReduce max marks per college](https://www.bctengineer.com/question.md?key=semester_8%2Fbig_data_technologies%3Amapreduce-max-marks-college) — Big Data Technologies, chapter 3

## Related in other subjects

- [8086 ALP: words on lines and word count](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-words-lines-count) — Microprocessor, chapter 3
- [Short note: Max-flow min-cut theorem](https://www.bctengineer.com/question.md?key=semester_4%2Fdiscrete_structure%3Amax-flow-min-cut-theorem) — Discrete Structure, chapter 4, asked 6×
- [Define αmax, αmin, insertion loss and gain](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Adefine-amax-amin-insertion-loss) — Filter Design, chapter 1, asked 2×
- [Informed vs uninformed; min-max; alpha-beta](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Ainformed-uninformed-minmax-alphabeta) — Artificial Intelligence, chapter 3
