Practice With Solutions

back to index  |  new

Show that the $(k+1)$ leading digits of the number $\underbrace{333\cdots 3}_{k}4^2$ are all $1$s. Here $k$ is any positive integer.


For any given integer $N$, show that there exists a perfect square whose leading digits match $N$. For example, if $N=2024$, there must exist a perfect square whose leading four digits are $2024$.


Solve $17^x-15^y=2$ in positive integers.


(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


Let $T=\underbrace{333\cdots 3}_{3^{2024}}$. Find the largest power of $3$ that can divid $T$.


Find all solutions in positive integers to $3^n = x^k + y^k$ where $x$ and $y$ are co-prime and $k\ge 2$.


Suppose $a$ and $b$ are both positive real numbers such as $a-b$, $a^2-b^2$, $a^3-b^3$, $\cdots$, are all positive integers. Show that $a$ and $b$ must be positive integers.


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]

 


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.