The most comprehensive library of algorithms in pure-functional standard Scala

Learn to think in Scala with pure-functional style algorithms

77 Scala Algorithms: 39 published, 6 fully free, 32 upcoming (one per week).

Browse the collection →

Each algorithm comes with:

  1. A description,
  2. An explanation,
  3. A proof or derivation, where appropriate,
  4. A list of relevant Scala concepts and explanations,
  5. An implementation in pure-functional immutable Scala, with efficiency in mind,
  6. And multiple test-cases that in the web-based IDE (no installation required!).
Screenshot of an example algorithm demonstrating the listed features
Example algorithm: MaximumProfitStockPrices

21 Scala concepts

  1. Class Inside Class
  2. Class Inside Def
  3. Collect
  4. Def Inside Def
  5. Drop, Take, dropRight, takeRight
  6. foldLeft and foldRight
  7. For-comprehension
  8. Lazy List
  9. Option Type
  10. Ordering
  11. Partial Function
  12. Pattern Matching
  13. Range
  14. scanLeft and scanRight
  15. Sliding / Sliding Window
  16. Stack Safety
  17. State machine
  18. Tail Recursion
  19. Type Class
  20. View
  21. Zip

77 Scala Algorithms

#1Balanced parentheses algorithm with tail-call recursion optimisation

Free

#2Check if an array is a palindrome

Free

#3Find an unpaired number in an array

Free

#4Count binary gap size of a number using tail recursion

Free

#5Find maximum potential profit from an array of stock price

Free

#6Fizz Buzz in purely functional immutable Scala

Free

#7Compute single-digit sum of digits

Free test cases & hints

#8Add numbers without using addition (plus sign)

Free test cases & hints

#9Tic Tac Toe MinMax solve

Free test cases & hints

#10Compute nth row of Pascal's triangle

Free test cases & hints

#11Print Alphabet Diamond

Free test cases & hints

#12Reverse a String's words efficiently

Free test cases & hints

#13Monitor success rate of a process that may fail

Free test cases & hints

#14QuickSelect Selection Algorithm (kth smallest item/kth order statistic)

Free test cases & hints

#15Rotate a matrix by 90 degrees

Free test cases & hints

#16Read a matrix as a spiral

Free test cases & hints

#17Count number of contiguous countries by colors

Free test cases & hints

#18Length of the longest common substring

Free test cases & hints

#19Find minimum missing positive number in a sequence

Free test cases & hints

#20Binary search a generic Array

Free test cases & hints

#21Run-length encoding (RLE) Decoder

Free test cases & hints

#22Run-length encoding (RLE) Encoder

Free test cases & hints

#23Find the contiguous slice with the minimum average

Free test cases & hints

#24Tic Tac Toe board check

Free test cases & hints

#25Find the minimum absolute difference of two partitions

Free test cases & hints

#26Compute a Roman numeral for an Integer, and vice-versa

Free test cases & hints

#27Quick Sort sorting algorithm in pure immutable Scala

Free test cases & hints

#28Fibonacci in purely functional immutable Scala

Free test cases & hints

#29Count numbers divisible by another

Free test cases & hints

#30Is an array a permutation?

Free test cases & hints

#31Merge Sort: stack-safe, tail-recursive

Free test cases & hints

#32Closest pair of coordinates in a 2D plane

Free test cases & hints

#33Counting inversions of a sequence

Free test cases & hints

#34Rotate Array right in pure-functional Scala

Free test cases & hints

#35Count factors of a number

Free test cases & hints

#36Merge Sort: in pure immutable Scala

Free test cases & hints

#37Longest increasing sub-sequence length

Free test cases & hints

#38Matching parentheses algorithm with foldLeft and a state machine

Free test cases & hints

#39Traverse a tree Breadth-First, immutably

Free test cases & hints

#40Count number of changes for making an anagram

Free test cases & hints

#41Check if a number is a palindrome

Free test cases & hints

#42Compute maximum sum of subarray (Kadane's algorithm)

Free test cases & hints

#43Check if a String is a palindrome

Free test cases & hints

#44Find sub-array with the maximum sum

Free test cases & hints

#45Traverse a tree Depth-First

Free test cases & hints

#46Find combinations adding up to N

Available 13th March

#47Remove duplicates from a sorted list (state machine)

Available 20th March

#48Remove duplicates from a sorted list (Sliding)

Available 27th March

#49Reshape a matrix

Available 3rd April

#50Find k closest elements to a value in a sorted Array

Available 10th April

#51Reverse bits of an integer

Available 17th April

#52Find the index of a substring ('indexOf')

Available 24th April

#53Binary search in a rotated sorted array

Available 30th April

#54Compute the length of longest valid parentheses

Available 1st May

#55Single-elimination tournament tree

Available 8th May

#56Make a queue using stacks (Lists in Scala)

Available 15th May

#57Make a queue using Maps

Available 15th May

#58Remove duplicates from an unsorted list

Available 22nd May

#59Count passing cars

Available 29th May

#60Pure-functional double linked list

Available 29th May

#61Check if a directed graph has cycles

Available 5th June

#62Longest common prefix of strings

Available 12th June

#63Check a directed graph has a routing between two nodes (depth-first search)

Available 28th June

#64Make a binary search tree

Available 5th July

#65Check a binary tree is a search tree

Available 12th July

#66Find height of binary tree

Available 19th July

#67Least-recently used cache (LRU)

Available 26th July

#68Reverse first n elements of a queue

Available 2nd August

#69Compute keypad possibilities

Available 9th August

#70Check a binary tree is balanced

Available 16th August

#71Establish execution order from dependencies

Available 23rd August

#72Count pairs of a given expected sum

Available 30th August

#73Compute modulo of an exponent without exponentiation

Available 6th September

#74Binary heap

Available 13th September

#75Compute the steps to transform an anagram only using swaps

Available 13th September

#76Binomial heap

Available 20th September

#77Game of Life

Available 28th September