expertfunda logo Top Interview Questions Binary Tree Binary Search Tree Graph

Leetcode Question Solution by ExpertFunda

Top Interview Question by ExpertFunda

Top Interview Question by ExpertFunda

Explore the best solutions to the top questions curated by ExpertFunda to help you ace technical interviews.

1. Two Sum

Problem: Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target.


public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> numMap = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (numMap.containsKey(complement)) {
            return new int[]{numMap.get(complement), i};
        }
        numMap.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}

2. Longest Substring Without Repeating Characters

Problem: Given a string s, find the length of the longest substring without repeating characters.


public int lengthOfLongestSubstring(String s) {
    Map<Character, Integer> charMap = new HashMap<>();
    int left = 0, maxLength = 0;
    for (int right = 0; right < s.length(); right++) {
        char current = s.charAt(right);
        if (charMap.containsKey(current)) {
            left = Math.max(charMap.get(current) + 1, left);
        }
        charMap.put(current, right);
        maxLength = Math.max(maxLength, right - left + 1);
    }
    return maxLength;
}

3. Merge Two Sorted Lists

Problem: Merge two sorted linked lists and return it as a new sorted list.


public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode current = dummy;
    while (l1 != null && l2 != null) {
        if (l1.val < l2.val) {
            current.next = l1;
            l1 = l1.next;
        } else {
            current.next = l2;
            l2 = l2.next;
        }
        current = current.next;
    }
    current.next = (l1 != null) ? l1 : l2;
    return dummy.next;
}

4. Maximum Subarray

Problem: Find the contiguous subarray within an array, nums, which has the largest sum.


public int maxSubArray(int[] nums) {
    int currentSum = nums[0];
    int maxSum = 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;
}

5. Valid Parentheses

Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.


public boolean isValid(String s) {
    Stack<Character> stack = new Stack<>();
    Map<Character, Character> mapping = new HashMap<>();
    mapping.put(')', '(');
    mapping.put('}', '{');
    mapping.put(']', '[');

    for (char c : s.toCharArray()) {
        if (mapping.containsKey(c)) {
            char topElement = stack.isEmpty() ? '#' : stack.pop();
            if (topElement != mapping.get(c)) {
                return false;
            }
        } else {
            stack.push(c);
        }
    }
    return stack.isEmpty();
}

Use the side menu to explore more top questions and their solutions.