Challenge: Find k Smallest Elements in a List

Given a list and a number "k" write a function that returns the first "k" smallest elements using a Heap.

Problem Statement

Implement a function findKSmallest(lst,k) that takes an unsorted integer list as input and returns the “k” smallest elements in the list using a Heap. The minHeap class that was written in Min Heap (Implementation) is prepended to this exercise so feel free to use it! Have a look at the illustration given for a clearer picture of the problem.

Output

Returns integer list that contains the first kk smallest elements from the given list.

Sample Input

lst = [9,4,7,1,-2,6,5]
k = 3

Sample Output

[-2,1,4]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.