Nth Smallest Integer

Published by Helen Yu in

Given an unsorted array, create a function that returns the nth smallest integer (the smallest integer is the first smallest, the second smallest integer is the second smallest, etc).

Examples

nth_smallest([1, 3, 5, 7], 1) ➞ 1

nth_smallest([1, 3, 5, 7], 3) ➞ 5

nth_smallest([1, 3, 5, 7], 5) ➞ nil

nth_smallest([7, 3, 5, 1], 2) ➞ 3

Notes

  • n will always be >= 1.
  • Each number in the array will be distinct (there will be a clear ordering).
  • Given an out of bounds parameter (e.g. an array is of size k), and you are asked to find the m > k smallest integer, return nil.
Watch a quick demo on how Edabit works.