Kth Smallest Element in an Unsorted Array
Given an unsorted array arr
of distinct integers and an integer k
, your task is to find the k-th
smallest element in the array.
Input:
The first line of input contains an integer T
denoting the number of test cases.
The first line of each test case contains two space-separated integers, n
and k
, where n
is the array's size.
The second line of each test case contains n
space-separated integers denoting the array elements.
Output:
For each test case, return the k-th
smallest element in the array.
Output for each query is printed in a separate line.
Example:
Input:
n = 5, k = 2 and arr[] = {6, 5, 4, 8, 7}
Output:
The array elements in sorted order are [4, 5, 6, 7, 8]. The ‘2-nd’ smallest element in the array is 5, so the answer is 5.
Constraints:
1 <= T <= 10
1 <= n <= 1000
1 <= k <= n
-10^6 <= arr[i] <= 10^6
Note:
1. Don’t print anything. Return the value of k-th
smallest element.
2. k
is a positive integer and not greater than the size of the array.
3. The array arr
is unsorted, and all the elements of the array are distinct.

AnswerBot
4mo
Find the k-th smallest element in an unsorted array of distinct integers.
Sort the array and return the k-th element.
Use a priority queue or quickselect algorithm for efficient solution.
Handle edge cas...read more
Help your peers!
Add answer anonymously...
Flipkart Software Developer interview questions & answers
A Software Developer was asked 6mo agoQ. Describe a problem that can be solved using the two-pointer technique.
A Software Developer was asked 9mo agoQ. Given the root of a binary search tree (BST) and an integer k, return the kth la...read more
A Software Developer was asked 9mo agoQ. Implement Depth First Search (DFS) traversal on a simple graph.
Popular interview questions of Software Developer
A Software Developer was asked 9mo agoQ1. Given the root of a binary search tree (BST) and an integer k, return the kth la...read more
A Software Developer was asked 9mo agoQ2. Implement Depth First Search (DFS) traversal on a simple graph.
A Software Developer was asked Q3. Describe a time you used topological sort to find the order of nodes in a graph.
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

