[Interview Question] — Find the largest and smallest word in a sentence!

Output —

Largest Word — occurring

Smallest Word — a

Try on your own and leave a reply containing your way of doing so!

Solution — Used Decoder (an Android app) to create this solution because I was lazy enough to not to turn on the laptop 😁


Step 1: Declare two variables to store largest and smallest word. Here assign the first word to the smallest variable because we want to make a comparison between words going forward (I have used array.split(‘ ‘) and picked word lying at the 0th index)

Step 2: Loop through each word and compare the length current iterating word with the initial word we are using (Remember we can find the length of string using .length just like we do for arrays)

For finding smallest word we are using currentWord.length < initialWord.length and for largest we are using currentWord.length > initialWord.length

Step 2: Reassign the word depending upon the Iterating word has smaller or larger length than the last Assigned Word length.

There maybe other far more optimised ways to achieve the same thing. I just shared my approach.

Do leave a comment how you should have gone for the solution.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store