Practice (171)

back to index  |  new

Given a pointer pointing to the header of a linked list, how to detect whether the linked list has a loop?

An additional question: how can math knowledge help here?


Given an string as input, find the length of the longest palindrome it contains. A palindrome is a word which stays the same if reading backwards. Examples include madam, Hannah. 


Determine whether a given integer can be expressed as the sum of two perfect squares.


(Coin Change Problem) Given a set of $N$ possible denominations of coins, find the number of different combinations to pay for a desired sum.


Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.


You are given an integer array height of length $n$. There are $n$ vertical lines drawn such that the two endpoints of the $i$th line are $(i, 0)$ and ($i$, height[$i$]). Find two lines that together with the $x$-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store.

Input: height = $[1,8,6,2,5,4,8,3,7]$

Output: $49$

Explanation: The above vertical lines are represented by array $[1,8,6,2,5,4,8,3,7]$. In this case, the max area of water (blue section) the container can contain is $49$.


A numeric array of length $N$ is given. We need to design a function that finds all positive numbers in the array that have their opposites in it as well. Describe approaches for solving optimal worst case and optimal average case performance, respectively.


Design an algorithm that finds the number of ways in which you can traverse $N$ meters by doing jumps of $1$, $2$, $3$, $4$, or $5$ meter lengths.


On a $M\times N$ board, some cells are occupied. Find the size of the largest square of unoccupied cells. 


Given $n$ non-negative integers representing an elevation map where the width of each bar is $1$, compute how much water it can trap after raining.

Example $1$:

Input: height = $[0,1,0,2,1,0,1,3,2,1,2,1]$

Output: $6$

Explanation: The above elevation map (black section) is represented by array $[0,1,0,2,1,0,1,3,2,1,2,1]$. In this case, $6$ units of rain water (blue section) are being trapped.

Example $2$:

Input: height = $[4,2,0,3,2,5]4

Output: $9$


Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. (Note that after backspacing an empty text, the text will continue empty.)

Example 1:

Input: s = "ab#c", t = "ad#c"

Output: true

Explanation: Both s and t become "ac".

Example 2:

Input: s = "ab##", t = "c#d#"

Output: true

Explanation: Both s and t become "".

Example 3:

Input: s = "a#c", t = "b"

Output: false

Explanation: s becomes "c" while t becomes "b".


You are given a string s consisting of lower case English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatedly make duplicate removal on s until we no longer can. Return the final string after all such duplicate removals have been made.

Example:

Input : s = "abbaca"

Output :  "ca"

1. remove bb

2. remove aa


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

Example 1:

Input: s = "abcabcbb"

Output: 3

Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"

Output: 1

Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"

Output: 3

Explanation: The answer is "wke", with the length of 3.

Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.


A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. (Note that s consists only of printable ASCII characters.)

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"

Output: false

Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "

Output: true

Explanation: s is an empty string "" after removing non-alphanumeric characters.

Since an empty string reads the same forward and backward, it is a palindrome.


Given a string s (containing only lower case English words), return true if the s can be palindrome after deleting at most one character from it.

Example 1:

Input: s = "aba"

Output: true

Example 2:

Input: s = "abca"

Output: true

Explanation: You could delete the character 'c'.

Example 3:

Input: s = "abc"

Output: false


Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. (Follow up: Could you do it in one pass?)

Example 1:

Input: head = [1,2,3,4,5], left = 2, right = 4

Output: [1,4,3,2,5]

Reasoning: 2 - 4 is [2,3,4]. We want to reverse it in place [4,3,2]. We do not modify the linked list elsewhere, giving us the final [1,4,3,2,5]

Example 2:

Input: head = [5], left = 1, right = 1

Output: [5]

 


You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.

 

Given the head of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr be a node with a child list. The nodes in the child list should appear after curr and before curr.next in the flattened list.

 

Return the head of the flattened list. The nodes in the list must have all of their child pointers set to null.

 

Example 1:

Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]

Output: [1,2,3,7,8,11,12,9,10,4,5,6]

 

Example 2:

Input: head = [1,2,null,3]

Output: [1,3,2]

 

 

Example 3:

Input: head = []

Output: []

Explanation: There could be empty list in the input.

 

Constraints:

The number of Nodes will not exceed 1000.

1 <= Node.val <= 105

How the multilevel linked list is represented in test cases:

 

We use the multilevel linked list from Example 1 above:

 

 1---2---3---4---5---6--NULL

         |

         7---8---9---10--NULL

             |

             11--12--NULL

The serialization of each level is as follows:

 

[1,2,3,4,5,6,null]

[7,8,9,10,null]

[11,12,null]

To serialize all levels together, we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:

 

[1,    2,      3, 4, 5, 6, null]

                 |

[null, null, 7,  8, 9, 10, null]

                      |

[            null, 11, 12, null]

Merging the serialization of each level and removing trailing nulls we obtain:

 

[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]


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

An input string is valid if:

  • Open brackets must be closed by the same type of brackets.
  • Open brackets must be closed in the correct order.
  • Every close bracket has a corresponding open bracket of the same type.

Example 1:

Input: s = "()"

Output: true

Example 2:

Input: s = "()[]{}"

Output: true

Example 3:

Input: s = "(]"

Output: false


Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if:

  • It is the empty string, contains only lowercase characters, or
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

Example 1:

Input: s = "lee(t(c)o)de)"

Output: "lee(t(c)o)de"

Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.

Example 2:

Input: s = "a)b(c)d"

Output: "ab(c)d"

Example 3:

Input: s = "))(("

Output: ""

Explanation: An empty string is also valid.