0% found this document useful (0 votes)
5 views12 pages

top array dsa

The document presents the top 10 most frequently asked data structure and algorithm questions with step-by-step explanations and full Java code implementations. Each question includes a brief description, code snippets, and explanations of the algorithms used, such as Two Sum, Kadane’s Algorithm, and others. It emphasizes the importance of practicing these questions to prepare for MNC interviews.

Uploaded by

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

top array dsa

The document presents the top 10 most frequently asked data structure and algorithm questions with step-by-step explanations and full Java code implementations. Each question includes a brief description, code snippets, and explanations of the algorithms used, such as Two Sum, Kadane’s Algorithm, and others. It emphasizes the importance of practicing these questions to prepare for MNC interviews.

Uploaded by

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

📘 **Top 10 Most Repeated Array DSA Questions with Step-by-Step

Explanations (English + Telugu with Full Java Code)**

### 1⃣ Two Sum

**Problem**: Find indices of two numbers that add up to a target.

```java

Import java.io.*;

Import java.util.*;

Class TwoSum {

Public static int[] twoSum(int[] nums, int target) {

For (int i = 0; i < nums.length; i++) { // loop 1: iterate first element

For (int j = i + 1; j < nums.length; j++) { // loop 2: check next


elements

If (nums[i] + nums[j] == target) {

Return new int[] { i, j }; // indexes return chesthundi

Return new int[] {}; // pair lekapothe empty return

Public static void main(String[] args) {

Int[] nums = {2, 7, 11, 15};

Int target = 9;

Int[] result = twoSum(nums, target);

System.out.println(Arrays.toString(result));
}

```

**Explanation**:

- Two loops run to check all pairs.

- రెండు loops ద్వారా ప్రతి possible pair ను check చేస్తాం.

- Sum == target అయితే index return.

- Time complexity: O(n^2)

### 2⃣ Kadane’s Algorithm

**Problem**: Find maximum subarray sum.

```java

Import java.io.*;

Class KadaneAlgo {

Public static int maxSubArray(int[] nums) {

Int maxSum = nums[0]; // Initialize maxSum

Int currentSum = nums[0]; // Initialize currentSum

For (int i = 1; i < nums.length; i++) {

currentSum = Math.max(nums[i], currentSum + nums[i]);

maxSum = Math.max(maxSum, currentSum); // max update


chesthunnam

Return maxSum;

}
Public static void main(String[] args) {

Int[] nums = {-2,1,-3,4,-1,2,1,-5,4};

System.out.println(maxSubArray(nums));

```

**Explanation**:

- ప్రతి step లో current element మరియు previous sum ను compare చేసి


max choose.

- Time: O(n)

### 3⃣ Sort 0s, 1s, 2s

```java

Import java.io.*;

Import java.util.*;

Class SortColors {

Public static void sortColors(int[] nums) {

Int low = 0, mid = 0, high = nums.length – 1;

While (mid <= high) {

If (nums[mid] == 0) {

Int temp = nums[low];

Nums[low++] = nums[mid];

Nums[mid++] = temp;
} else if (nums[mid] == 1) {

Mid++;

} else {

Int temp = nums[mid];

Nums[mid] = nums[high];

Nums[high--] = temp;

Public static void main(String[] args) {

Int[] nums = {2, 0, 2, 1, 1, 0};

sortColors(nums);

System.out.println(Arrays.toString(nums));

```

**Explanation**:

- Dutch National Flag algorithm use chestam.

- Low, mid, high ane 3 pointers use cheyadam.

### 4⃣ Merge Two Sorted Arrays

```java

Import java.util.*;

Class MergeSortedArrays {
Public static void merge(int[] nums1, int m, int[] nums2, int n) {

Int i = m – 1, j = n – 1, k = m + n – 1; // end nunchi merge

While (i >= 0 && j >= 0) {

If (nums1[i] > nums2[j]) {

Nums1[k--] = nums1[i--];

} else {

Nums1[k--] = nums2[j--];

While (j >= 0) {

Nums1[k--] = nums2[j--];

Public static void main(String[] args) {

Int[] nums1 = {1,2,3,0,0,0};

Int[] nums2 = {2,5,6};

Merge(nums1, 3, nums2, 3);

System.out.println(Arrays.toString(nums1));

```

**Explanation**:

- Merge cheyyadam ending nunchi jarugutundi.

- Time: O(m + n)
### 5⃣ Move Zeros to End

```java

Import java.util.*;

Class MoveZeros {

Public static void moveZeroes(int[] nums) {

Int index = 0;

For (int i = 0; i < nums.length; i++) {

If (nums[i] != 0) {

Nums[index++] = nums[i];

While (index < nums.length) {

Nums[index++] = 0;

Public static void main(String[] args) {

Int[] nums = {0, 1, 0, 3, 12};

moveZeroes(nums);

System.out.println(Arrays.toString(nums));

```

**Explanation**:

- Non-zero values front ki shift chestam.


- Remaining places ni 0 tho fill chestam.

### 6⃣ Find Duplicate Number

```java

Class FindDuplicate {

Public static int findDuplicate(int[] nums) {

Int slow = nums[0];

Int fast = nums[0];

Do {

Slow = nums[slow];

Fast = nums[nums[fast]];

} while (slow != fast);

Slow = nums[0];

While (slow != fast) {

Slow = nums[slow];

Fast = nums[fast];

Return slow;

Public static void main(String[] args) {

Int[] nums = {1,3,4,2,2};

System.out.println(findDuplicate(nums));

}
```

**Explanation**:

- Floyd’s cycle detection use chestam.

- Duplicate = cycle starting point

### 7⃣ Trapping Rain Water

```java

Class TrappingRainWater {

Public static int trap(int[] height) {

Int left = 0, right = height.length – 1;

Int leftMax = 0, rightMax = 0, water = 0;

While (left < right) {

If (height[left] < height[right]) {

If (height[left] >= leftMax)

leftMax = height[left];

else

water += leftMax – height[left];

left++;

} else {

If (height[right] >= rightMax)

rightMax = height[right];

else

water += rightMax – height[right];

right--;

}
}

Return water;

Public static void main(String[] args) {

Int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};

System.out.println(trap(height));

```

**Explanation**:

- Water = min(leftMax, rightMax) – height

- Trap calculation chestam left/right borders tho.

### 8⃣ Kth Largest Element

```java

Import java.util.*;

Class KthLargest {

Public static int findKthLargest(int[] nums, int k) {

Arrays.sort(nums);

Return nums[nums.length – k]; // kth largest element

Public static void main(String[] args) {

Int[] nums = {3,2,1,5,6,4};


Int k = 2;

System.out.println(findKthLargest(nums, k));

```

**Explanation**:

- Sort chesi kth position pick chestam.

### 9⃣ Maximum Product Subarray

```java

Class MaxProductSubarray {

Public static int maxProduct(int[] nums) {

Int max = nums[0], min = nums[0], result = nums[0];

For (int i = 1; i < nums.length; i++) {

Int temp = max;

Max = Math.max(nums[i], Math.max(max * nums[i], min *


nums[i]));

Min = Math.min(nums[i], Math.min(temp * nums[i], min *


nums[i]));

Result = Math.max(result, max);

Return result;

Public static void main(String[] args) {

Int[] nums = {2,3,-2,4};


System.out.println(maxProduct(nums));

```

**Explanation**:

- Negative numbers valla min product kuda track chestam.

### 🔟 Subarray with Given Sum (Positive only)

```java

Class SubarraySumPositive {

Public static int subarraySum(int[] nums, int sum) {

Int currSum = nums[0], start = 0;

For (int i = 1; i <= nums.length; i++) {

While (currSum > sum && start < i – 1) {

currSum -= nums[start++];

If (currSum == sum) return 1;

If (i < nums.length) currSum += nums[i];

Return 0;

Public static void main(String[] args) {

Int[] nums = {1, 4, 20, 3, 10, 5};

Int sum = 33;

System.out.println(subarraySum(nums, sum));
}

```

**Explanation**:

- Sliding window technique use chestam.

- Sum match ayithe return.

📌 **Tip**: Ee 10 questions perfect ga practice cheste, most MNC


interviews confidently crack cheyyachu! ✅

You might also like