Shubham Kumar - Resume - Software Developer

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Describe how you could solve the following problem:

Given n unsorted integers, we want the top k integers. No other information is


given other than 0 < k < n.

Ans-
To find the top k integers in an unsorted list of n integers, we can use a
variation of the quicksort algorithm. The typical quicksort algorithm operates with
an average time complexity of O(n log n), but by using a modified version, we can
achieve O(n) time complexity for finding the top k integers.
Here's a high-level overview of the algorithm:
1. 1-Choose a pivot element from the list.
2. 2-Partition the list into two sublists: one containing elements greater than the
pivot and the other containing elements less than the pivot.
3. 3-If the size of the sublist containing elements greater than the pivot is
greater than or equal to k, recursively apply the same process to this sublist.
4. 4-If the size of the sublist containing elements greater than the pivot is less
than k, recursively apply the process to the sublist containing elements less than
the pivot, while reducing k accordingly.
5. 5-Stop the recursion when you have found the top k integers.

Given a list of sortable values we want to remove the top k. No other information
is given other than 0 < k < n. Describe an efficient O(n) algorithm for doing this.

Ans-
To efficiently remove the top k values from a list of sortable values while
maintaining a time complexity of O(n), we can follow these steps:
6. Find the k-th largest element:
• Use a selection algorithm to find the k-th largest element in the list.
This algorithm can be based on techniques like Quickselect, which has an average
time complexity of O(n).
• Once you find the k-th largest element, let's denote it as x.
7. Partition the list:
• Partition the list into two parts based on the k-th largest element x.
Elements greater than or equal to x should be on the left, and elements smaller
than x should be on the right.
• This partitioning step can be done in O(n) time.
8. Remove the elements:
• After partitioning, the elements greater than or equal to x will be at the
beginning of the list, and they will constitute the top k values.
• You can simply remove the first k elements from the list, which will give
you the list with the top n−k values.
• Removal of the top k elements takes O(k) time.

Given a list of integers we want to remove the top k integers. No other information
is given other than 0 < k < n. Describe an efficient algorithm for doing this.
Ans-
To efficiently remove the top k integers from a list of integers, we can use a
variation of the selection algorithm known as Quickselect. This algorithm allows us
to find the k-th largest element in linear time complexity on average O(n)).
Here's a more understandable way to describe the algorithm:
9. Quickselect Algorithm:
• Choose a pivot element from the list of integers. This pivot can be
selected randomly or using a strategy like selecting the median of the array.
• Partition the list around the pivot, such that elements less than the pivot
are on the left and elements greater than the pivot are on the right.
• If the pivot's index is equal to n−k, then the pivot is the k-th largest
element, and all elements to its right are the top k integers.
• If the pivot's index is less than n−k, then the top k integers lie to the
right of the pivot. In this case, recursively apply the Quickselect algorithm on
the right subarray.
• If the pivot's index is greater than n−k, then the top k integers lie to
the left of the pivot. In this case, recursively apply the Quickselect algorithm on
the left subarray, while adjusting k accordingly.
10. Removing the Top k Integers:
• After finding the k-th largest element using Quickselect, partition the
array around this element.
• The elements greater than the k-th largest element will be on its left
side, constituting the top k integers.
• Simply remove the first k elements from the list, and you'll be left with
the list containing the remaining integers after removing the top k integers.
This algorithm ensures efficient removal of the top k integers from the list of
integers with a time complexity of O(n), making it suitable for large lists where n
is the number of integers in the list.

Describe an efficient O(n) algorithm to find the Kth largest element in an array.

Ans-
To find the k-th largest element in an array efficiently with a time complexity of
O(n), we can use a modified version of the Quickselect algorithm. Quickselect is a
selection algorithm that is similar to Quicksort, but it only partially sorts the
data instead of fully sorting it.
Here's a more detailed description of the efficient O(n) algorithm to find the k-th
largest element:
11. Choose a Pivot Element:
• Choose a pivot element from the array. The pivot selection can be random or
deterministic. Common strategies include selecting the first, last, or middle
element of the array, or choosing a random element.
12. Partition the Array:
• Partition the array around the chosen pivot element. This step rearranges
the elements of the array such that all elements less than the pivot are on the
left, and all elements greater than the pivot are on the right.
• After partitioning, the pivot element is in its final sorted position if
the array were sorted. Let's denote its index as p.
13. Recursive Procedure:
• Compare p with k:
○ If p=k−1, then the pivot element is the k-th largest element in the
array. Return the pivot element.
○ If p<k−1, then the k-th largest element lies to the right of the
pivot. Recursively apply the algorithm to the right subarray with the adjusted k.
○ If p>k−1, then the k-th largest element lies to the left of the
pivot. Recursively apply the algorithm to the left subarray with the original k.
14. Repeat the Process:
• Repeat the process recursively until the k-th largest element is found.
15. Base Case:
• The base case occurs when the subarray to search is of size 1. In this
case, the k-th largest element is the element itself.
16. Termination:
• The algorithm terminates when the k-th largest element is found.
This algorithm works by recursively partitioning the array around a pivot element
and narrowing down the search space until the k-th largest element is found. The
time complexity of this algorithm is O(n) on average, making it efficient for
finding the k-th largest element in large arrays. However, in the worst case, the
time complexity can degrade to O(n2), but this is rare and occurs when poorly
chosen pivot elements consistently create unbalanced partitions.

List the following asymptotic complexities from best (top) to worst. Which
functions are the same rank?

𝞱( n lg n )

𝞱( n2 )

𝞱( lg n )

𝞱( nn )

𝞱( log n )

𝞱( √n )

𝞱( n √n )

𝞱( lg lg n )

𝞱( n! )

𝞱( 1 )

𝞱( n lg n2 )

𝞱( n lg2 n )

𝞱( n )

𝞱( n2 lg n )

𝞱( nlg n )

𝞱( n3 lg n )

𝞱( n3 )

Ans-
Here are the complexities listed from best to worst:
17. Θ(1)
18. Θ(logn)
19. Θ(n)
20. Θ(loglogn)
21. Θ(nlogn)
22. Θ(nlog2n)
23. Θ(nlogn2)
24. Θ(nlog2n+nlogn2)
25. Θ(nlogn+nlogn)
26. Θ(nlog2n+3n)
27. Θ(nlogn2+nlog2n)
28. Θ(nn)
29. Θ(n2)
30. Θ(n2logn)
31. Θ(n3)
32. Θ(n3logn)
33. Θ(n!)
34.

Rank these 1-5 (Fastest = 1, Slowest = 5):

____ 𝞱( nlg n )
____ 𝞱( n2 lg n )
____ 𝞱( n lg2 n )
____ 𝞱( n lg n2 )
____ 𝞱( lgn n )
____ 𝞱( lg nn )
____ 𝞱( n lg n )

Ans-
So the ranking would be:
35. Θ(nlogn)(fastest)
36. Θ(nlogn)
37. Θ(nlog2n)
38. Θ(nlog2n)
39. Θ(logn⋅logn)(slowest)

Rank these 1-7 (Fastest = 1, Slowest = 7):

____ 𝞱( nlg n )
____ 𝞱( lgn n )
____ 𝞱( lg nn )
____ 𝞱( n lg n )
____ 𝞱( n2 lg n )
____ 𝞱( n lg2 n )
____ 𝞱( n lg n2 )

Ans-
So the ranking would be:
Θ(nlogn)(fastest)
40. Θ(logn⋅n)
41. Θ(logn⋅logn)
42. Θ(nlogn)
43. Θ(n2logn)
44. Θ(nlog2n)
45. Θ(nlogn)(slowest)

Reduce these and list them smallest to largest. Note any equalities by listing them
together as the same rank.

𝞱( n lg2 n + n lg n2 )
𝞱( nlg n + n2 lg n )
𝞱( lgn n + lg nn )
𝞱( n lg n + nlg n)
𝞱( lg2 n + 3n)
Ans-

Smallest to largest:
46. Θ(logn⋅n+logn⋅logn)
47. Θ(n⋅logn+n⋅logn)
48. Θ(logn⋅n+n⋅logn)
49. Θ(logn⋅n+n2⋅logn)
50. Θ(log2n+3n)

Θ(logn⋅n+n⋅logn) (same as) Θ(n⋅logn+n⋅logn))

You might also like