Text Justification Problem

Problem Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. OR Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. ...

Merge Overlapping Intervals

Problem Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. OR Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals. Examples Example 1: Input: intervals = [1,3],[2,6],[8,10],[15,18], Output: [1,6],[8,10],[15,18]. ...

First Missing Positive Problem

Problem Given an unsorted integer array, find the first missing positive integer. Follow up You have to find the smallest positive number missing from the array in O(n) time using constant extra space. You can modify the original array. Examples Example 1: Input: nums = [1,2,0] Output: 3 ...

Search in Rotated Sorted Array

Problem Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Follow up: what if duplicates are allowed? ...

Design LRU Cache

Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and add. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. The functions get and put must each run in O(1) average time complexity. ...

Remove Nth Node From End of List

Problem Given a linked list, remove the nth node from the end of list and return its head. Examples Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Explanation: Given linked list 1->2->3->4->5 and n = 2, the result is 1->2->3->5. ...

Letter Combinations of a Phone Number Problem

Problem Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. ...

Regular Expression Matching Problem

Problem Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Examples Some examples: isMatch("aa","a") ? false isMatch("aa","a.") ? true isMatch("aaa","ab*") ? false isMatch("aa", ".*") ? true isMatch("aa", "a*") ? true isMatch("ab", ".*") ? true isMatch("aabaa", "a*b.*") ? false ...

Min Stack Problem

Problem Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. You must implement a solution with O(1) aka constant time complexity for each function. ...

Second largest element in binary search tree

Problem Given a BST (Binary search tree), find the second largest element of it. Examples Example 1: 1 / \ 0 2 ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy