Java Interview Programs for LTIMindtree
Java Interview Programs for LTIMindtree
The algorithm counts occurrences of a specific character in a string using Java Streams. It converts the string into an IntStream of characters and filters those matching the target character, then counts the matches. This stream processing method is efficient due to its declarative nature, allowing precise and concise data processing and parallell optimization .
The technique used to remove duplicates from an array involves using a LinkedHashSet to store elements. This data structure inherently ensures that only unique elements are retained and maintains the insertion order. The array elements are iterated over and added to the LinkedHashSet, automatically filtering out duplicates .
The algorithm checks if a number is prime by iterating from 2 to the square root of the number and checking for divisibility. It is efficient because it reduces the number of iterations necessary to find factors, taking advantage of the property that a larger factor of a number must be paired with a smaller factor less than or equal to the square root .
The document determines leap years using a boolean condition that checks if a year is evenly divisible by 4, not evenly divisible by 100, or evenly divisible by 400. This conditional logic aligns with the Gregorian calendar rules for leap years, ensuring correct identification by addressing exceptions that occur every 100 years .
Inheritance is demonstrated through a class 'Dog' extending another class 'Animal.' The purpose is to allow the 'Dog' class to reuse and extend the functionality of the 'Animal' class, promoting code reuse and a hierarchical structuring of classes. It allows 'Dog' to access methods of 'Animal,' like 'eat,' along with its own methods like 'bark' .
To detect an Armstrong number, the algorithm calculates the sum of each digit raised to the power of three (since it checks for a three-digit number) and compares this sum to the original number. The fundamental dependency of the algorithm is on the definition of an Armstrong number, where the sum of the nth powers of its digits equals the number itself .
The code calculates the factorial of a number using recursion by defining a method that calls itself with the value decremented by one. The base case used is when the number is 0 or 1, returning 1 in these cases to terminate the recursion and provide a value for further multiplications up the call stack .
The process to identify a missing number in a sequence involves calculating the expected sum of the entire sequence using the formula for the sum of an arithmetic series, and then subtracting the sum of the provided sequence. The mathematical concept it relies on is the sum formula for a series, n(n + 1)/2, which represents the sum of the first n natural numbers .
The code's efficiency in swapping two numbers without a temporary variable leverages three arithmetic operations: addition and subtraction. This works based on arithmetic properties, where adding and then subtracting values provides a means of swapping without external storage; however, it assumes no integer overflow .
The code checks whether a given string is a palindrome by reversing the string and comparing it to the original. It uses a StringBuilder to reverse the string and the equals method to check equality. The major criterion it relies on is that a palindrome reads the same forward and backward, making the reversed string identical to the original if it is a palindrome .