DSA - 1

Modified on Tue, 28 Mar, 2023 at 4:50 PM

Why are my test cases failing when searching for elements in a sorted array?

Do you understand the error?

Can we check what kind of test cases are failing? What are perf-test cases?

Where is the issue we are having?

Can you check the loop and see what we are doing there? How are we finding the index?

Why do you think you are seeing this issue? What might be causing it?

perf-test cases fail if the code is not optimized according to the problem statement. What is the TC of the above approach?

How to Debug this? To debug this issue, I would recommend the following steps:

Is there any other way we can search for an element? Do you know how to implement a binary search? 

Do you know this?

Binary search is a search algorithm that is used to search for an element in a sorted array. It works by repeatedly dividing the search interval in half. If the target element is less than the middle element, the search continues in the left half of the array. If the target element is greater than the middle element, the search continues in the right half of the array. This process is repeated until the target element is found or determined to be not present in the array. Binary search has a time complexity of O(log n), which makes it an efficient search algorithm for large arrays.

Resource: Binary Search 


Getting the wrong output when trying to find the most frequent element?

Do you understand the error?

 What is the output you’re getting? Can we check the output on a small test case and see what’s the difference between expected and actual outputs?

Where is the error occurring?

 We might see that error is occurring where we are storing the most frequent element.

Why do you think you are seeing this error? What might be causing it?

What are the conditions that we are storing and updating our most frequent element? Are there any other conditions we are missing?

How to Debug this? To debug this issue, I would recommend the following steps:

 When a most frequent element is asked, we are also given an additional condition which needs to be checked when the frequency of 2 elements is the same.

If a problem is asking you to find the most frequent thing and it has multiple items with the same occurrence(frequency) then you will be asked to check another property like ASCII or value, once check the problem and see if it has mentioned any such and try to make those changes. 

Let's say we have two numbers with frequency {8: 3, 10: 3, 15:6}. Here we have 2 numbers with frequency 3 i.e 8 and 10, so if the problem asks us to return the lower one answer will be 8 and if it asks for the higher number answer will be 10.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article