Fork me on GitHub

Project Notes

#402 hungryBears

Using Clojure to find the bears that are more hungry than average; cassidoo’s interview question of the week (2026-01-12).

Notes

The interview question of the week (2026-01-12) asks us to find the bears that are more hungry than average:

Given an array of objects representing bears in a forest, each with a name and hunger level, return the names of all bears whose hunger level is above the forest average, sorted alphabetically. In how few lines can you do this one?

Example:

const bears = [
  { name: 'Baloo', hunger: 6 },
  { name: 'Yogi', hunger: 9 },
  { name: 'Paddington', hunger: 4 },
  { name: 'Winnie', hunger: 10 },
  { name: 'Chicago', hunger: 20 },
];

hungryBears(bears)
> ['Chicago', 'Winnie']

Thinking about the Problem

There are three elements required of the solution:

  • determine the average hunger
  • find bears with hunger > average
  • sort the bear names

In ruby-ish pseudo-code, this could be a one-line map-reduce type problem:

bears.select { |bear| bear['hunger'] > bears.average('hunger') }.map { |bear| bear['name'] }.sort

A first approach

I’m doing this in Clojure just to make my life difficult.

But it turns out that Clojure can do a pretty good job of translating my pseudo-code:

  • calculate the average hunger with a map-reduce
  • filter the list of bears by hunger
  • map the name and sort

See source src/challenge.clj; there are no dependencies required:

(ns challenge)

(defn hungry-bears [bears]
  (let [avg (/ (reduce + (map :hunger bears)) (count bears))]
    (->> bears (filter #(> (:hunger %) avg)) (map :name) sort)))


(defn run [opts]
  (println
    (hungry-bears
      [
        {:name "Baloo" :hunger 6}
        {:name "Yogi" :hunger 9}
        {:name "Paddington" :hunger 4}
        {:name "Winnie" :hunger 10}
        {:name "Chicago" :hunger 20}
      ]
    )))

And the result is as expected:

$ clj -X challenge/run
(Chicago Winnie)

Credits and References

About LCK#402 Clojurecassidoo

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader