How to find the longest possible quasi-constant subsequence of array?

This is my task2 challenge. I’ve tried to solve this question, but my result wasn’t good. Although there was no error after testing, I only got 33% score.😰

The summary of question shows as below:

I was given a non-empty unsorted array such as: A = [6, 10, 6, 9, 7, 8]. The amplitude of a subsequence of array A is the difference between the largest and the smallest element in this subsequence. The amplitude of the empty subsequence is assumed to be 0.

These are some of the subsequences of array A and their amplitudes:
[6, 6, 7] has amplitude 1;
[6, 10, 6, 9, 7, 8] has amplitude 4;
[6, 6, 7, 8] has amplitude 2.

If this subsequence of array is called quasi-constant, it means its amplitude does not exceed 1. In the example above, subsequence [6, 6, 7] is the longest possible quasi-constant subsequence of array A.

Now, you are expected to provide a method that takes in an array and returns the maximum number of quasi-constant subsequence of array. In above example, the function should return 3.

閱讀更多How to find the longest possible quasi-constant subsequence of array?

My first Codility test experience

Last week, I received an email from a recruiter who asked me to do Codility assessment. I had to answer three questions within 245 minutes. Although I’ve heard about Codility, this was my first Codility test experience. To be honest, it was really difficult to me.

These questions were not easy to understand, so it took me some time to understand each question. Then I tried to answer the questions by Java or Python programming language. Even though I eventually found the solutions and summited the answers, my average score was not good enough. When I finished this assessment, I was very disappointed with myself. I even thought whether I should keep a programmer job.😂😂😂

It seems that more and more companies like to use Codility assessments to evaluate developers, but I don’t think it is the best way to test a programmer’s coding/programmer abilities.

閱讀更多My first Codility test experience

[Java] Quick Sort Algorithm

Quick sort is a very fast sort algorithm, which it’s used “divide and conquer strategy” to sort the data. On the average and best case, it has O(n log n) time complexity, on worst case, it has O(n^2) time complexity. And it is unstable sorting.

閱讀更多[Java] Quick Sort Algorithm

[Java] Selection Sort Algorithm

Time complexity of Selection Sort Algorithm in best case , worst case and average are all O(n^2) and need O(1) extra space. Selection sort can be implemented in stable sort or not.

閱讀更多[Java] Selection Sort Algorithm

[Java] Binary search algorithm

Binary search algorithm time complexity is O(log2(n)), and this algorithm requires that source array is sorted in order to work correct.

Algorithm is quite simple. It can be done either recursively or iteratively:

1.get the middle element;
2.if the middle element equals to the searched value, the algorithm stops;
3.otherwise, two cases are possible:
   (1)searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.
   (2)searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.

閱讀更多[Java] Binary search algorithm