Author: akshay
Question: Reverse Integer with Overflow Check Write a function reverse(x) that takes a 32-bit signed integer and returns its digits reversed. If the reversed value overflows the 32-bit signed integer range, return 0. Requirements Reverse the digits of a 32-bit signed integer. Return 0 if the result overflows. Constraints Time Complexity: O(d) where d is the number of digits. Space Complexity: O(1) — constant space. Example Input: 123 Output: 321 Input: -123 Output: -321 Input: 1534236469 Output: 0 (overflow) Step-by-Step Approach Preserve the Original: Save x in xCopy. Work with Absolute Value: Use Math.abs(x) or abs(x) to simplify reversal. Reverse…
Question: Find the Second Largest Number in an Array Write a function secondLargest(arr) that takes an array of numbers and returns the second largest unique number in the array. Requirements The array must contain at least two numbers. If the array contains all equal elements or only one unique element, return: “No second largest found” If the array has less than two elements, return: “Array should have at least two numbers” Constraints Time Complexity: O(n) (Single pass through the array) Space Complexity: O(1) Examples Input: [0, 3, 5, 2, 7, 9] Output: 7 Input: [4, 4, 4, 4] Output: “No…
Question Write a function that returns the number of negative numbers in an array. Approach Initialize a counter to 0. Loop through the array. If the element is less than 0, increment the counter. Return the final count after the loop ends. Example Input: arr = [2, -6, 4, 8, 1, -9] Output: 2 Time & Space Complexity Time Complexity: O(n) – where n is the number of elements in the array. Space Complexity: O(1) – only a counter variable is used. JavaScript C C++ Java Python function countNegativeNumbers(arr) { let count = 0; for (let i = 0; i…
Question Write a function that returns the largest number in an array. Approach Initialize a variable largest to -Infinity. Loop through the array. If the current element is greater than largest, update largest. Return largest after the loop ends. Example Input: arr = [2, -6, 4, 8, 1, -9] Output: 8 Time & Space Complexity Time Complexity: O(n) – where n is the number of elements in the array. Space Complexity: O(1) – only one variable is used. JavaScript C C++ Java Python function findLargest(arr) { let largest = -Infinity; for (let i = 0; i < arr.length; i++) {…
Question Write a function that returns the smallest number in an array. Approach Initialize a variable smallest to Infinity. Loop through the array. If the current element is less than smallest, update smallest. Return smallest after the loop ends. Example Input: arr = [2, -6, 4, 8, 1, -9] Output: -9 Time & Space Complexity Time Complexity: O(n) – where n is the number of elements in the array. Space Complexity: O(1) – only one variable is used. JavaScript C C++ Java Python function findSmallest(arr) { let smallest = Infinity; for (let i = 0; i < arr.length; i++) {…
Question Write a function that searches for an element in an array and returns its index. If the element is not found, return -1. Approach Loop through the array from start to end. If the current element matches the search target, return its index. If no match is found by the end, return -1. Example Input: arr = [2, 6, 4, 8, 1, 9], x = 2 Output: 0 Input: arr = [2, 6, 4, 8, 1, 9], x = 7 Output: -1 Time and Space Complexity Time Complexity: O(n), where n is the number of elements in the array.…
Question Write a program to print all even numbers from an array. Example array: [10, 3, 5, 2, 7, 6, 9] Expected Output: 10 2 6 Approach Iterate through each element in the array. Check if the element is divisible by 2. If yes, print the element (it’s even). Example Input: [10, 3, 5, 2, 7, 6, 9] Output: 10 2 6 JavaScript C C++ Java Python let arr = [10, 3, 5, 2, 7, 6, 9]; for (let i = 0; i < arr.length; i++) { if (arr[i] % 2 === 0) { console.log(arr[i]); } } #include <stdio.h> int…
Question Write a function that accepts a number and checks whether it is even or odd. If the number is divisible by 2 (i.e., remainder is 0), it’s an Even number. Otherwise, it’s an Odd number. Test the function with inputs 18 and 5. Approach Accept the input number in the function. Check if the number modulo 2 equals 0. If yes, print or return “Even”. Otherwise, print or return “Odd”. Test the function with different numbers to verify correctness. Example Input: 18 Output: Even Input: 5 Output: Odd JavaScript C++ C Java Python function isEvenOdd(num) { let rem =…
Question Write a function that accepts a person’s age and prints whether the person is: “Invalid input” if the age is less than 1. “Not eligible to vote” if the age is less than 18. “Eligible to vote” if the age is 18 or above. Call the function with different test values: 18, 0, and 8. Approach To solve this problem, follow these steps: Accept the input age in the function. Check if the age is less than 1; if yes, print “Invalid input”. If the age is valid but less than 18, print “Not eligible to vote”. If the…
Question Write a function that takes an integer and returns its square. Call this function and print the result. square(x) is a function that computes the square of a number. It returns the result instead of printing it. square(3) returns 9, which is then printed. Examples Input: 3 – Output: 9 Input: 5 – Output: 25 Input: 10 – Output: 100 Approach To calculate the square of a number, follow these steps: Accept the input integer x. Multiply x by itself to get the square. Return the result from the function. Call the function with the desired number and print…
Contact Us
Subscribe to Stay Updated
Stay ahead in the world of tech with our exclusive newsletter! Subscribe now for regular updates on the latest trends, valuable coding resources, and tips to boost your frontend development skills.
