basic/intermediate Flashcards

1
Q

Palindrome Check:
Write a program to check if a given string is a palindrome.

A

Check Characters from Both Ends

Normalize the String:
Convert the string to lowercase to ensure case-insensitivity.
Remove all non-alphanumeric characters (spaces, punctuation) using a regular expression.

Two-pointer Technique:
Initialize two pointers: left starting at the beginning (0), and right starting at the end of the string (s.length() - 1).

Check Characters from Both Ends:
Use a while loop to compare characters from both ends until the pointers meet or cross each other.
If the characters at the left and right pointers do not match, return false.
If the characters match, move the left pointer to the right (left++) and the right pointer to the left (right–).

Determine if the String is a Palindrome:
If the loop completes without finding any mismatched characters, return true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fibonacci Series:
Write a program to print the first ‘n’ numbers of the Fibonacci series.

A

n is the sum of previous 2 numbers

Recursion:
Base Case: Handle the simplest cases directly.
Recursive Case: Calculate the Fibonacci number by summing the results of two smaller subproblems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Factorial Calculation:
Write a program to calculate the factorial of a given number using recursion and iteration.

A

factorial of n is the multiplication of all numbers leading to it

Recursion:
Base Case: Handles the simplest case directly (n == 0 or n == 1).
Recursive Call: The function calls itself with a smaller argument (n - 1).
Stack Usage: Each recursive call adds a new frame to the call stack, which can lead to stack overflow for large n.

Iteration:
Initialization: Starts with a base result of 1.
Loop: Uses a for loop to multiply the result by each integer up to n.
Memory Efficiency: Uses a constant amount of memory (no additional stack frames).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Prime Number Check:
Write a program to check if a given number is prime.

A

Check if the number is divisible by any number except 1 and itself

Special Cases: Check if the number is less than or equal to 1. If so, it’s not a prime number.
Trial Division: Check for divisors from 2 up to the square root of the number. If the number is divisible by any of these, it’s not a prime number.
Efficiency: Only check up to the square root of the number to reduce the number of iterations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Reverse a String:
Write a program to reverse a given string.

A

Swap first and last characters

Two pointer:
Initialize Pointers: Set one pointer (left) at the start of the string and another pointer (right) at the end of the string.
Swap Characters: Swap the characters at the left and right pointers.
Move Pointers: Increment the left pointer and decrement the right pointer.
Continue Until Pointers Meet: Repeat the process until the left pointer is greater than or equal to the right pointer.

Iterative:
Initialize: Create an empty result string.
Iterate: Loop through the original string from the last character to the first character.
Build: Append each character to the result string.

StringBuilders Reverse method:
Initialize: Create a StringBuilder with the original string.
Reverse: Use the reverse() method.
Convert: Convert the StringBuilder back to a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Remove Duplicates from Array:
Write a program to remove duplicates from an array.

A

Add it to a set and convert to array

Initialize a HashSet: Create a HashSet to store unique elements.
Iterate through the Array: Add each element to the HashSet.
Convert Set to Array: Convert the HashSet back to an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Find Second Largest Element:
Write a program to find the second largest element in an array.

A

Loop through the array to check if current number is second largest based on conditions

Initialize Variables: Create two variables, firstLargest and secondLargest, to store the largest and second largest elements, respectively.
Iterate through the Array: Loop through the array to find the largest and second largest elements.
Update Variables: Update firstLargest and secondLargest based on the current element in the iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Anagram Check:
Write a program to check if two strings are anagrams of each other.

A

process chararrays, If any count is negative after processing the char array second string, it means the second string had more of that character than the first, indicating the strings are not anagrams.

Using Character Frequency:
Normalize Strings: Convert both strings to lowercase.
Count Frequencies: Use an array to count the frequency of each character.
Compare Frequencies: Compare the frequency arrays for both strings.

compare sorted strings and check if anagram
Sorting and Comparing
Normalize Strings: Convert both strings to lowercase (to ensure the comparison is case-insensitive) and remove any non-alphanumeric characters if necessary (though typically anagrams concern only alphabetical characters).
Sort Strings: Sort the characters of both strings.
Compare Strings: Compare the sorted strings. If they are identical, the original strings are anagrams.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Sum of Digits:
Write a program to find the sum of the digits of a given number.

A

This can be done by repeatedly extracting the last digit of the number and adding it to a sum variable, then removing the last digit from the number until the number is reduced to zero.

Approach
Initialize a Sum Variable: Start with a sum variable initialized to 0.
Extract and Add Digits: Use a loop to extract the last digit of the number and add it to the sum.
Remove the Last Digit: Use integer division by 10 to remove the last digit.
Continue Until the Number is Zero: Repeat the process until the number becomes zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Leap Year Check:
Write a program to check if a given year is a leap year.

A

The rules to determine whether a year is a leap year are as follows:

A year is a leap year if it is divisible by 4.
However, if the year is also divisible by 100, it is not a leap year.
But if the year is divisible by 400, it is a leap year.

Steps
Divisibility by 4: Check if the year is divisible by 4.
Divisibility by 100: If it is divisible by 4, then check if it is also divisible by 100.
Divisibility by 400: If it is divisible by 100, then check if it is also divisible by 400.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

String Concatenation:
Write a program to concatenate two strings without using the + operator.

A

concatenate strings without using the + operator is to use the concat method of the String class.

We can use the StringBuilder class, which provides an efficient way to concatenate strings.
Initialize a StringBuilder: Create a StringBuilder instance.
Append Strings: Use the append method of StringBuilder to concatenate the strings.
Convert to String: Convert the StringBuilder instance back to a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Odd or Even:
Write a program to check if a given number is odd or even.

A

The logic to determine if a number is odd or even is simple:
A number is even if it is divisible by 2 (i.e., number % 2 == 0).
A number is odd if it is not divisible by 2 (i.e., number % 2 != 0).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Vowel Count:
Write a program to count the number of vowels in a given string.

A

Loop through each character and check if vowel

Initialize a Counter: Create a counter variable to keep track of the number of vowels.
Iterate Through the String: Loop through each character in the string.
Check for Vowels: For each character, check if it is a vowel.
Increment the Counter: If the character is a vowel, increment the counter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Largest of Three Numbers:
Write a program to find the largest of three numbers.

A

Intuition
The intuition behind finding the largest of three numbers is to compare the numbers with each other using conditional statements. By systematically comparing the numbers, we can determine the largest one.

Approach
Input the three numbers: Read the three numbers from the user or define them in the code.
Compare the numbers:
First, compare the first number with the second and third numbers.
If the first number is greater than or equal to both the second and third numbers, then the first number is the largest.
Otherwise, compare the second number with the third number.
If the second number is greater than or equal to the third number, then the second number is the largest.
If neither of the above conditions is met, then the third number is the largest.
Output the result: Print the largest number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Swapping Variables:
Write a program to swap two variables without using a third variable.

A

Swapping two variables without using a third variable can be achieved using arithmetic operations

Addition and Subtraction: We can use addition and subtraction to swap the values of two variables.
Add both variables and store the result in one of them.
Subtract the other variable from the updated variable to get the original value of the first variable.
Subtract the first variable from the updated variable to get the original value of the second variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Sum of Array Elements:
Write a program to find the sum of all elements in an array.

A

The intuition behind finding the sum of all elements in an array is to traverse the array and keep adding each element to a running total. This can be achieved easily using a loop

Approach
Initialize the Array: Define the array with the given elements.
Initialize the Sum Variable: Create a variable to store the cumulative sum of the elements.
Iterate Through the Array: Use a loop to traverse each element in the array.
Add Each Element to the Sum: Inside the loop, add the current element to the sum variable.
Output the Sum: After the loop completes, print the sum.

15
Q
A