Hello, everyone! Today, we will solve one of the coding test problems using JavaScript, which is the “Finding Non-Perfect Squares” problem. This problem is commonly encountered in developer interviews and requires an understanding of basic algorithmic thinking and the fundamental syntax of JavaScript.
Problem Description
Write a function that filters out and returns only the non-perfect square numbers from a given array of numbers.
A perfect square refers to a number that can be expressed as n*n for some integer n. For example, 1, 4, 9, 16, and 25 are, respectively, the squares of 1, 2, 3, 4, and 5.
Input and Output
- Input: An array of integers (e.g., [1, 2, 3, 4, 5, 6])
- Output: An array composed of non-perfect square numbers (e.g., [2, 3, 5, 6])
Examples
Input: [1, 2, 3, 4, 5, 6] Output: [2, 3, 5, 6]
Input: [9, 10, 11, 12, 13, 14] Output: [10, 11, 12, 13, 14]
Problem Solving Process
Step 1: Understand the Problem
First, let’s clarify the requirements to understand the problem. We will receive an array of integers and need to find the numbers that are not perfect squares from this array. To determine if a number is a perfect square, we can calculate the square root of each number and check whether it is an integer.
Step 2: Analyze the Examples
Let’s check which numbers are perfect squares using the given examples. For instance, in the array [1, 2, 3, 4, 5, 6]
, the perfect squares are 1
and 4
. The remaining numbers 2, 3, 5, 6
are not perfect squares and should be included in the result array.
Step 3: Think of a Solution
We can use the following method to solve the problem:
- Iterate through each number and check if it is a perfect square.
- If a number exists such that n*n equals the integer n, then that number is a perfect square.
- Add the non-perfect square numbers to a new array.
- Finally, return the new array.
Step 4: Implement the Code
Based on the methods discussed above, let’s write the JavaScript code.
function isPerfectSquare(num) {
const sqrt = Math.sqrt(num);
return sqrt === Math.floor(sqrt);
}
function findNonPerfectSquares(arr) {
return arr.filter(num => !isPerfectSquare(num));
}
// Example tests
console.log(findNonPerfectSquares([1, 2, 3, 4, 5, 6])); // [2, 3, 5, 6]
console.log(findNonPerfectSquares([9, 10, 11, 12, 13, 14])); // [10, 11, 12, 13, 14]
Step 5: Explain the Code
In the code above, we defined two functions:
isPerfectSquare(num)
: This function checks whether the given number is a perfect square. It computes the square root and compares it with the original number after removing the decimal part.findNonPerfectSquares(arr)
: This function filters out the non-perfect square numbers from the given array and returns them as a new array. It uses theArray.filter()
method to find the non-perfect squares.
Step 6: Consider Performance
The time complexity of this code is O(n). Since we check each element of the array once, the performance depends linearly on the length of the array in the worst case. This algorithm is efficient enough and should perform well in real-world problems.
Step 7: Handle Various Test Cases
Finally, let’s utilize additional test cases to solve this problem:
- Edge case: What should be the output for an empty array
[]
? – It should return an empty array[]
. - Including negative numbers: For
[-1, -4, 3, 8]
, the non-perfect squares are-1, 3, 8
. - Changing array: For
[0, 1, 2, 3, 16, 25]
, the non-perfect squares are[2, 3]
.
Conclusion
Today, we solved the “Finding Non-Perfect Squares” problem. Through this problem, we gained an understanding of basic array processing and the mathematical concept of perfect squares. We learned how to solve problems using the fundamental syntax of JavaScript and array methods.
While preparing for coding tests, it is essential to practice various types of problems. By solving multiple problems, you can establish a fundamental understanding of algorithms and improve your problem-solving skills. In the next lesson, we will tackle even more interesting problems!
Thank you!