Hardest Coding Problem Explained (LeetCode)

preview_player
Показать описание
Here is a step by step explanation of the hardest coding problem on LeetCode.

Check out my interview prep platform for learning the patterns!

The "valid number" problem on LeetCode has a 14.7% acceptance rate making it one of the hardest coding interview questions asked by large tech companies. Both Facebook and LinkedIn have asked this question multiple times to candidates in the past 6 months.

The reason this problem is so difficult is because it requires a fair amount of code to handle many different edge cases. In order for a number to be considered valid in this problem, we must handle faulty cases when iterating over our input string. We only care about characters that are decimals, e's, signs, or digits. Any other character that shows up in our input string will immediately make it not a valid number.
Рекомендации по теме
Комментарии
Автор

You explain this so well that a beginner can inplement this into his/her starter language

homieinthesky
Автор

there is already a function that checks this problem but here an implementation on C

#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>

bool is_decimal_number(const char *str) {
if (str == NULL) {
return false;
}

// Check if the string is empty
if (*str == '\0') {
return false;
}

// Check if the string starts with a negative sign
if (*str == '-') {
str++;
}

// Check if the string contains only digits
while (*str != '\0') {
if (!isdigit(*str)) {
return false;
}
str++;
}

return true;
}


int main(void) {
const char *str1 = "123";
const char *str2 = "-123";
const char *str3 = "123.45";
const char *str4 = "abc";

printf("%d\n", is_decimal_number(str1)); // prints 1
printf("%d\n", is_decimal_number(str2)); // prints 1
printf("%d\n", is_decimal_number(str3)); // prints 0
printf("%d\n", is_decimal_number(str4)); // prints 0

return 0;
}

MyVipr
Автор

Difficult problem easily explained, great job and kudos for this.

maxwellmaragia
Автор

I went and cheeked the same issue in leet code, and was able to fix it without a for-loop
just validate if the string was null or ends with e, or + or .
then try to cast it to decimal, if casting failed, exit with false (it worked)


if (s == "Infinity" || s == "-Infinity" || s == "+Infinity") return false;
... more validations
try:
double num = double.Parse(s);
catch:
return false;

mamyname
Автор

Hi Guys! I may not be professional in writing code. But I solved it within 2 minutes. I used Python's Exception Handling concept here, by float(input string) if it gives value error just return false. Trust me It really worked 😂😂😂

pshivaramakrishna
Автор

Accepted.

class Solution:
def isNumber(self, s: str) -> bool:
s = s.lower()
try:
if 'inf' in s:
return False
x = float(s)
return True
except:
return False

Runtime: 32 ms, faster than 79.16% of Python3 online submissions for Valid Number.
Memory Usage: 14.1 MB, less than 84.88% of Python3 online submissions for Valid Number.

nishadtardalkar
Автор

(constant space complexity)
“Memory Usage: less than 6.25% of submissions”

da fuq everybody doin over there

okiseeyou
Автор

I would definitely say that you have understood the problem thoroughly and thanks for letting us know the way you understood. Thanks mate.

sampleshawn
Автор

If anyone wants to study together or make a new friend, feel free to dm me! It would be awesome to go over any programming concepts or Leetcode together!

satoshinakamoto
Автор

You can solve it with a regex and e counter (I'm new to CS and from mobile so it's pretty bad):
Bool isValidDecimal(string str) {
Regex reg = /[-+]?\d+(\.\d+?(e -? \d+))?/;
Return reg.test(str) ;
}
[-+]? A sign in the start and its optional
\d+ must be at least one digit before the decimal point
The parentheses and question sign mean that all the part of decimal point is option
\. We're escaping the dot because Normally dot in regex means every character and now it means just dot
\d+? There might be numbers before the exponent
(e -? \d+)? There might be not exponent and if there is there must be just one and after the decimal point and afterwards might be a minus sign and there must be at least one digit
I really didn't think about it that much and there might be some cases I missed and rook me forever ro type cuz of mobile so correct me if I'm wrong, I'm also only 5 months into coding so yeah

harelavv
Автор

Regardless of difficulty let us agree it is just a bad coding question.

DanielVazquez
Автор

you explain really well, good job! Thanks for the vidéo :)

oli
Автор

Another hard problem made easy!
great explanation!

theghostwhowalk
Автор

this problem is not so hard, it is just very poorly written and you don't know what is considered valid and what not until you have attempted it many times.
I had to attempt it nine times, and each time I found out a new edge case what is considered valid or not valid, until the tenth time I finally got it.

shmuelmoskowicz
Автор

Awesome explanation.. keep up the good work.

sanjayizardar
Автор

Nice explain, but i still not understand 😅 iwant to learn coding but, i hope i can 😅

yukkiamatsu
Автор

why not just convert it to parseInt or parseDouble and handle exceptions to check instead?

paoloranit
welcome to shbcf.ru