0% found this document useful (0 votes)
15 views

Prep - Coding

The document presents a series of programming questions and solutions related to string manipulation and array operations. It includes algorithms and Java code examples for tasks such as reversing strings, finding duplicates, checking anagrams, and various array operations like summing elements, finding maximum/minimum values, and checking if an array is sorted. The content is aimed at preparing for technical interviews by providing practical coding challenges and their solutions.

Uploaded by

avp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Prep - Coding

The document presents a series of programming questions and solutions related to string manipulation and array operations. It includes algorithms and Java code examples for tasks such as reversing strings, finding duplicates, checking anagrams, and various array operations like summing elements, finding maximum/minimum values, and checking if an array is sorted. The content is aimed at preparing for technical interviews by providing practical coding challenges and their solutions.

Uploaded by

avp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

12 Important Questions Solve yourself

1. How do you reverse a given string in place?


2. How do you print duplicate characters from a string?
3. How do you check if two strings are anagrams of each other?
4. How do you find all the permutations of a string?
5. How do you find duplicate characters in a given string?
6. How do you count several vowels and consonants in a given string?
7. How do you count the occurrence of a given character in a string?
8. How do you print the first non-repeated character from a string?
9. How do you check if a given string is a palindrome?
10. how to remove the duplicate character from String?
11. How to find the maximum occurring character in a given String?
12. How do you remove a given character from String?

Arrays - 10 Problem and Solutions – Read at a Glance before


Interview

1. Sum of Elements in an Array:

Algorithm:

1. Initialize a variable sum to 0.

2. Iterate through each element in the array.

3. Add each element to the sum.

4. Print the final sum.

public class SumOfArray {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : numbers) {
sum += num;
}
System.out.println("Sum of elements: " + sum);
}
}
2. Find Maximum and Minimum in an Array:

Algorithm:

1. Initialize variables max and min with the first element of the array.

2. Iterate through each element in the array.

3. If the current element is greater than max, update max.

4. If the current element is less than min, update min.

5. Print the final values of max and min.

public class MaxMinArray {


public static void main(String[] args) {
int[] numbers = {7, 2, 9, 1, 5};
int max = numbers[0];
int min = numbers[0];
for (int num : numbers) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
System.out.println("Maximum: " + max);
System.out.println("Minimum: " + min);
}
}

3. Check if Array Contains a Specific Element:

Algorithm:

1. Initialize a boolean variable contains to false.

2. Iterate through each element in the array.

3. If the current element is equal to the target element, set contains to true and break out of
the loop.

4. Print whether the array contains the target element based on the value of contains.

public class ArrayContainsElement {


public static void main(String[] args) {
int[] numbers = {3, 6, 9, 12, 15};
int target = 9;
boolean contains = false;
for (int num : numbers) {
if (num == target) {
contains = true;
break;
}
}
if (contains) {
System.out.println("Array contains " + target);
} else {
System.out.println("Array does not contain " + target);
}
}
}

4. Reverse an Array:

Algorithm:

1. Initialize variables start to 0 and end to the last index of the array.

2. Use a while loop with the condition start < end to swap elements at positions start and end.

3. Increment start and decrement end in each iteration.

4. Print the reversed array.

public class ReverseArray {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int start = 0;
int end = numbers.length - 1;
while (start < end) {
// Swap elements at start and end indices
int temp = numbers[start];
numbers[start] = numbers[end];
numbers[end] = temp;
// Move indices towards the center
start++;
end--;
}
// Print reversed array
for (int num : numbers) {
System.out.print(num + " ");
}
}
}

5. Linear Search: Find Index of an Element in an Array:

Algorithm:

1. Initialize a variable target with the element to search.

2. Iterate through each element in the array.

3. If the current element is equal to the target, return the current index.

4. If the loop completes without finding the element, return -1.


public class LinearSearch {
public static void main(String[] args) {
int[] numbers = {5, 8, 2, 1, 9, 3};
int target = 9;

int index = linearSearch(numbers, target);


if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found in the array.");
}
}
private static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
}

6. Check if an Array is Sorted:

Algorithm:

1. Iterate through each element in the array.

2. Compare the current element with the next element.

3. If any element is greater than the next one, the array is not sorted.

4. If the loop completes without finding such elements, the array is sorted.

public class CheckSortedArray {


public static void main(String[] args) {
int[] numbers = {1, 3, 5, 7, 9};
if (isSorted(numbers)) {
System.out.println("The array is sorted.");
} else {
System.out.println("The array is not sorted.");
}
}
private static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
return false;
}
}
return true;
}
}

7. Remove Duplicate Elements from an Array:

Algorithm:

1. Initialize an empty set to keep track of unique elements.

2. Iterate through each element in the array.

3. If the element is not in the set, add it to the set and print it.

import java.util.HashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] numbers = {3, 7, 2, 8, 7, 3, 1, 2};

removeDuplicates(numbers);
}
private static void removeDuplicates(int[] arr) {
HashSet<Integer> uniqueSet = new HashSet<>();
for (int num : arr) {
if (uniqueSet.add(num)) {
System.out.print(num + " ");
}
}
}
}

8. Count Occurrences of Each Element in an Array:

Algorithm:

1. Initialize a HashMap to store element frequencies.

2. Iterate through each element in the array.

3. For each element, update its frequency in the HashMap.

4. Print the element and its frequency.

import java.util.HashMap;
public class CountOccurrences {
public static void main(String[] args) {
int[] numbers = {3, 7, 2, 8, 7, 3, 1, 2};
countOccurrences(numbers);
}
private static void countOccurrences(int[] arr) {
HashMap<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : arr) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
for (int num : frequencyMap.keySet()) {
System.out.println("Element: " + num + ", Frequency: " + frequencyMap.get(num));
}
}
}

9. Rotate Array to the Right by K Steps:

Algorithm:

1. Calculate the effective rotation by taking the remainder when dividing k by the array length
(k % arr.length).

2. Reverse the entire array.

3. Reverse the subarray from index 0 to k - 1.

4. Reverse the subarray from index k to the end of the array.

public class RotateArray {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int k = 2;
rotateArray(numbers, k);
for (int num : numbers) {
System.out.print(num + " ");
}
}
private static void rotateArray(int[] arr, int k) {
k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
private static void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}

10. Find Leaders in an Array:

Algorithm:

1. Initialize a variable max to the last element of the array.

2. Iterate through the array from right to left.

3. For each element, if it’s greater than max, update max and print the element.
public class LeadersInArray {
public static void main(String[] args) {
int[] numbers = {16, 17, 4, 3, 5, 2};
findLeaders(numbers);
}
private static void findLeaders(int[] arr) {
int max = arr[arr.length - 1];
System.out.println("Leader: " + max);
for (int i = arr.length - 2; i >= 0; i--) {
if (arr[i] > max) {
max = arr[i];
System.out.println("Leader: " + max);
}
}
}
}

11. Find the Missing Number:

Given an array containing n distinct numbers taken from the range 0 to n, find the one missing from
the array.

Algorithm:

1. Calculate the expected sum of the first n natural numbers using the formula n * (n + 1) / 2

2. Calculate the actual sum of the elements in the array using a ‘for’ loop.

3. Subtract the actual sum from the expected sum.

4. The result is the missing number.

public int findMissingNumber(int[] nums) {


int n = nums.length + 1;
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;
for (int num : nums) {
actualSum += num;
}
return expectedSum - actualSum;
}

You might also like