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

LeetCode_Interview_Questions_Solutions_Improved

The document lists 100 commonly asked LeetCode interview questions along with their Java solutions, categorized into sections such as Array Questions, String Questions, Dynamic Programming, and Linked List. Each section includes specific problems like 'Two Sum', 'Maximum Subarray', and 'Reverse Linked List', providing concise code implementations for each. This resource serves as a guide for preparing for technical interviews focused on algorithmic challenges.

Uploaded by

Jayanth Chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LeetCode_Interview_Questions_Solutions_Improved

The document lists 100 commonly asked LeetCode interview questions along with their Java solutions, categorized into sections such as Array Questions, String Questions, Dynamic Programming, and Linked List. Each section includes specific problems like 'Two Sum', 'Maximum Subarray', and 'Reverse Linked List', providing concise code implementations for each. This resource serves as a guide for preparing for technical interviews focused on algorithmic challenges.

Uploaded by

Jayanth Chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

100 Most Asked LeetCode Interview Questions and Solutions in Java

1. Array Questions

--------------------

1.1 Two Sum

public int[] twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

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

if (map.containsKey(target - nums[i])) {

return new int[] { map.get(target - nums[i]), i };

map.put(nums[i], i);

return new int[] {};

1.2 Best Time to Buy and Sell Stock

public int maxProfit(int[] prices) {

int minPrice = Integer.MAX_VALUE;

int maxProfit = 0;

for (int price : prices) {

minPrice = Math.min(minPrice, price);

maxProfit = Math.max(maxProfit, price - minPrice);

return maxProfit;
}

1.3 Contains Duplicate

public boolean containsDuplicate(int[] nums) {

Set<Integer> set = new HashSet<>();

for (int num : nums) {

if (!set.add(num)) {

return true;

return false;

1.4 Product of Array Except Self

public int[] productExceptSelf(int[] nums) {

int n = nums.length;

int[] result = new int[n];

result[0] = 1;

for (int i = 1; i < n; i++) {

result[i] = result[i - 1] * nums[i - 1];

int right = 1;

for (int i = n - 2; i >= 0; i--) {

right *= nums[i + 1];

result[i] *= right;

}
return result;

1.5 Maximum Subarray

public int maxSubArray(int[] nums) {

int maxSum = nums[0];

int currentSum = nums[0];

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

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

maxSum = Math.max(maxSum, currentSum);

return maxSum;

2. String Questions

--------------------

2.1 Reverse String

public void reverseString(char[] s) {

int left = 0, right = s.length - 1;

while (left < right) {

char temp = s[left];

s[left] = s[right];

s[right] = temp;

left++;

right--;

}
}

2.2 Valid Anagram

public boolean isAnagram(String s, String t) {

if (s.length() != t.length()) return false;

int[] count = new int[26];

for (int i = 0; i < s.length(); i++) {

count[s.charAt(i) - 'a']++;

count[t.charAt(i) - 'a']--;

for (int c : count) {

if (c != 0) return false;

return true;

2.3 Longest Substring Without Repeating Characters

public int lengthOfLongestSubstring(String s) {

Set<Character> set = new HashSet<>();

int left = 0, maxLen = 0;

for (int right = 0; right < s.length(); right++) {

while (set.contains(s.charAt(right))) {

set.remove(s.charAt(left));

left++;

set.add(s.charAt(right));
maxLen = Math.max(maxLen, right - left + 1);

return maxLen;

2.4 Palindrome

public boolean isPalindrome(String s) {

int left = 0, right = s.length() - 1;

while (left < right) {

if (!Character.isLetterOrDigit(s.charAt(left))) {

left++;

} else if (!Character.isLetterOrDigit(s.charAt(right))) {

right--;

} else if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {

return false;

} else {

left++;

right--;

return true;

2.5 Zigzag Conversion

public String convert(String s, int numRows) {

if (numRows == 1) return s;
StringBuilder[] rows = new StringBuilder[numRows];

for (int i = 0; i < numRows; i++) {

rows[i] = new StringBuilder();

int currentRow = 0;

boolean goingDown = false;

for (char c : s.toCharArray()) {

rows[currentRow].append(c);

if (currentRow == 0 || currentRow == numRows - 1) {

goingDown = !goingDown;

currentRow += goingDown ? 1 : -1;

StringBuilder result = new StringBuilder();

for (StringBuilder row : rows) {

result.append(row);

return result.toString();

3. Dynamic Programming

--------------------

3.1 Climbing Stairs

public int climbStairs(int n) {

if (n == 1) return 1;

int first = 1, second = 2;


for (int i = 3; i <= n; i++) {

int temp = first + second;

first = second;

second = temp;

return second;

3.2 Maximum Subarray

public int maxSubArray(int[] nums) {

int maxSum = nums[0];

int currentSum = nums[0];

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

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

maxSum = Math.max(maxSum, currentSum);

return maxSum;

4. Linked List

--------------------

4.1 Reverse Linked List

public ListNode reverseList(ListNode head) {

ListNode prev = null, current = head;

while (current != null) {

ListNode nextTemp = current.next;


current.next = prev;

prev = current;

current = nextTemp;

return prev;

You might also like