Even though I have passed the test, I feel my implementation is not optimised or has not been making use of existing library functions. Is there a referenced answer that I can learn from?
Sorry, no there is not.
The purpose of the assignment is to learn how it works by implementing the code. There may be libraries that do this task faster or easier, but they would not provide much learning opportunity.