Fork me on GitHub

Project Notes

#481 longestSorted

Using ruby to find the longest word whose letters appear in alphabetical order; cassidoo’s interview question of the week (2026-09-14).

Notes

The interview question of the week (2026-09-14):

Given a sentence, return the longest word whose letters appear in alphabetical order.

Examples:

> longestSorted("The autumn leaves almost glow.")
> "almost"

> longestSorted("A cool sheep sleeps.")
> ""

Thinking about the Problem

A couple of things to note about the examples:

  • we are given sentences, with leading capitalisation and punctuation. So let’s assume we need to ignore case and punctuation.
  • “A” is not retuned as the answer in the second case, so assume we only consider words with more than 1 character.

Other than that, this should be straight-forward:

  • tokenise the words (>1 char, ignore case, strip punctuation)
  • filter for only those whose letters appear in alphabetical order
  • find the longest

A First Go

Let’s treat this as 3 stages:

  • split the sentence into words, lower-case, stripped of punctuation
  • filter for only words over 1 char that have letters in alphabetical order
  • get the longest
  def words
    sentence.split.map { |word| word.downcase.gsub(/[^a-z]/, '') }
  end

  def alphabetically_sorted_words
    words.select { |word| word.length > 1 && word == word.chars.sort.join }
  end

  def longestSorted
    alphabetically_sorted_words.max_by { |word| word.length  } || ""
  end

And that works:

$ ./challenge.rb
Usage: ruby ./challenge.rb <sentence>
$ ./challenge.rb "The autumn leaves almost glow."
Sentence: "The autumn leaves almost glow."
Result: "almost"
$ ./challenge.rb "A cool sheep sleeps."
Sentence: "A cool sheep sleeps."
Result: ""

Could this be optimised? Yes, ofc:

  • could be a one-liner
  • rather than map all words and then find the longest, could short circuit that by mapping and selecting in one pass

Tests

I’ve setup some validation in test_challenge.rb:

$ ./test_challenge.rb
Run options: --seed 39519

# Running:

....

Finished in 0.000270s, 14814.8149 runs/s, 14814.8149 assertions/s.

4 runs, 4 assertions, 0 failures, 0 errors, 0 skips

Example Code

Final code is in challenge.rb:

#!/usr/bin/env ruby

class Challenge
  attr_accessor :sentence

  def initialize(sentence)
    self.sentence = sentence
  end

  def words
    sentence.split.map { |word| word.downcase.gsub(/[^a-z]/, '') }
  end

  def alphabetically_sorted_words
    words.select { |word| word.length > 1 && word == word.chars.sort.join }
  end

  def longestSorted
    alphabetically_sorted_words.max_by { |word| word.length  } || ""
  end
end

if __FILE__ == $PROGRAM_NAME
  (puts "Usage: ruby #{$0} <sentence>"; exit) unless ARGV.length == 1
  sentence = ARGV[0]
  calculator = Challenge.new(sentence)
  puts "Sentence: #{calculator.sentence.inspect}"
  puts "Result: #{calculator.longestSorted.inspect}"
end

Credits and References

About LCK#481
Rubycassidoo

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