Category: Data Structures

  • Delete Node in a Linked List given only the node

    Problem: You are given a node to delete from a linked list. But you are not given the head node. Delete it in O(1) time and O(1) space complexity. Constraints: For example, Given the linked list : 1 -> 4 -> 7 -> 8 And the node 7 to be deleted The output should be…

  • Course Schedule II

    Problem : Given an integer array representing prerequisites of different courses and the total number of courses , find out the order in which the courses should be taken. For example, if the given input is [1,0] It means to take course 1 you should have completed course 0. Or in other words, Course 0…

  • Odd Even Linked List

    Problem: Given the head of a linked list , reorder the linked list so that the elements in odd indices are grouped together followed by the elements in event indices. The first index is assumed to be 1 (odd) and the second index even and so on. For example, For the below input: 1 ->…

  • Find first and last position of element in sorted array

    Problem: Given a sorted integer array find the first and last position of given target element For example, If given array is [ 1,3,6,6,7,9] and target is 6 Then output is [2,3] which represents the first and last index position of the element 6 For the same input if the target is 8 Then the…

  • Find the next highest permutation of an array

    Problem: Given an integer array , find the next highest permutation of the numbers. If the next highest permutation is not found return the smallest permutation of the given input. Permutation of a number is all possible combinations of the given numbers. For example, Given the array: [1,2,3] The possible permutations are [1,3,2] , [2,1,3],…

  • Find if a linked list is a palindrome or not

    Problem: Given the head of a linked list , find if it is a palindrome or not. Find the solution in O(n) time and in place (O(1) space complexity) For example , the below linked list : 1 -> 2 -> 2 -> 1 is a palindrome The below is not: 1 -> 5 ->…

  • Convert Excel Sheet Column Title to Number

    Problem: Given an excel sheet column title , convert it to the column value. For example, Column value of sheet number ‘A’ is 1 ‘Z’ is 26 ‘AA’ is 27 ‘BA’ is 53 and so on. Constraints: Try out the solution here; https://leetcode.com/problems/excel-sheet-column-number/ Solution: If it is a single letter we can return the value…

  • First Unique Character in a String

    Given a string find the index of the first unique character (non repeating). For example, For the string “see” the output is 0 since the letter s is not repeated in the string For the string “bbaaddavt” the output is 7 because the letter v is not repeated and is present in index 7. Assumption:…

  • Find first missing positive integer in an array.

    Problem: Given an array of integers find the first missing positive integer . The first positive integer is 1. So if 1 is itself missing you need to return 1 else whatever number after 1 is missing that should be returned. For example, Given the array: [1,2,4,5] The output is 3 Given the array: [3,4,-1,1,5]…

  • Kth largest element in an array

    Given an array of integers, find the kth largest element in the array. For example, Given the input [3,2,1,5,6,4] and k = 2 The output is 5 Try out the solution here: https://leetcode.com/problems/kth-largest-element-in-an-array/ Solution: We can solve this problem in multiple ways. Let’s look at three approaches. Approach 1 – Sort the array In this…