Author: devangini123
π Problem Statement: 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 0if the result overflows. Constraints: Time Complexity: O(d)Where dis the numberof digits. Space Complexity: O(1) β Constant space. Example: Input:123 Output:321 Input:-123 Output:-321 Input:1534236469 Output:0 (overflow) Approach: Preserve the Original: SavexinxCopy. Work with Absolute Value: UseMath.abs(x)orabs(x)to simplify reversal. Reverse Digits: Intializerev = 0.Whilex != 0 : last = x % 10 rev = rev * 10 + last x //=…
π Problem Statement: Write a function isPalindrome(x) that takes an integer x and returns true if it reads the same backward and forward; otherwise false. Requirements: Handles both positive and negative integers. Return falsefor negative numbers (not Palindromes). Constraints: Time Complexity: O(d)Where dis the number of digits. Space Complexity: O(1)Only a few variables are used. Examples: Input:121 Output:true Input:-121 Output:false Input:10 Output:false Approach: Handle Negatives:If x < 0, return false. Store OriginalSave the input inxCopyfor comparison. Reverse:Intializerev = 0 while x > 0 : rem = x % 10 rev = rev * 10 + rem x //= 10 Compare:If…
π Problem Statement: Write a function countDigits(n)that takes an integer n and returns how many digits it contains. Requirements: Handles both positive and negative integers. Return 1 if n is 0(since 0 is a single-digit number). Examples: Input:259 Output:3 Input:-1035 Output:4 Input:0 Output:1 Approach: Handle Zero:If n == 0, return 1 directly. Convert to Positive:Use abs(n) to ignore sign. Initialize a counter:Set count = 0. Loop:While n > 0 Divide n by 10 using integer division. Increment count. Return: The count after the loop finishes. Visualisation: JavaScript Python Java C++ C C# function countDigits(n) { if (n === 0) return…
π What is Time Complexity? Time complexity measures how efficient an algorithm is as the input size increases. It’s not the same as the actual time taken to run a program. Time Complexity != Execution Time Linear vs Binary Search Linear Search Best Case: Element at 1st index β 1 operation Average Case: Element at n/2 index β n/2 operations Worst Case: Element not found β n operations Time Complexity: O(n) Requirement: Can work on unsorted arrays Binary Search Best Case: Middle element matched β 1 operation Average Case: logβ(n) operations Worst Case: logβ(n) operations Time Complexity: O(log n) Requirement:…
π Problem Statement: Write a function secondLargest(arr) that returns the second largest distinct number in an array. Requirements: The array must contain at least two elements. If all elements are equal, return: No second largest found. If the array has fewer than two elements, return: Array should have at least two numbers. Examples: Input: arr = [0, 3, 5, 2, 7, 9] β Output: 7 Input: arr = [4, 4, 4, 4] β Output: No second largest found Input: arr = [5] β Output: Array should have at least two numbers Input: arr = [10, 20] β Output: 10 Constraints:…
π Problem Statement: Write a function that returns the largest number in an array. Approach: Initialize a variablelargestto -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 a counter variable is used. Visualisation: JavaScript Python Java C++ C C# function findLargest(arr) { let largest = -Infinity; for (let i = 0; i < arr.length; i++) {…
π Problem Statement: 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 a single variable is used. Visualisation: JavaScript Python Java C++ C C# function findSmallest(arr) { let smallest = Infinity; for (let i = 0;…
π Problem Statement: 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. Visualisation: JavaScript Python Java C++ C C# function countNegativeNumbers(arr) { let count = 0; for (let…
π Problem Statement: Write a program to print all even numbers from an array. Example: Input: [10, 3, 5, 2, 7, 6, 9] 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). Visualisation: JavaScript Python Java C++ C C# 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]); } } arr = [10, 3, 5, 2, 7, 6, 9] for num in arr:…
π Problem Statement: Write a function that accepts a number and checks whether it is Even or Odd. If the number is divisble by 2, it’s an Even number. Otherwise, it’s an Odd number. Test the function with inputs 18 and 5. Example Input: 18 β Output: Even Number Input: 5 β Output: Odd Number Approach Create a function that takes a number. If number % 2 === 0, return “Even”. Else return “Odd”. Visualisation: Explanation: β Accept the input number in the function. β Check if the number modulo 2 equals 0. β If yes, print or return “Even”.…
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.
