Google Just Hit an Absolute Low - Easy Interview Question - Check if Sentence is Pangram - 1832

preview_player
Показать описание
FAANG Coding Interviews / Data Structures and Algorithms / Leetcode
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

This solution will not give you a pass from Google.

At bare minimum, you would be expected to terminate as soon as the the set size is 26- not iterate through the whole sentence before checking. You can often return true before the whole sentence (which can be len(1000) is checked.

Too many of your vids give solutions that would fail people.

sophiophile
Автор

Using hashmaps or sets will produce lot of cache misses. Using fixed size array in the stack is the fastest way to go.

Tamtam-hhxv
Автор

U don't even need a set u could just bitwise OR a 32 bit integer by a bit shifted left x - 'a' times for all x in the str and see if it equals or whatever

alechenderson
Автор

manipulating the crowd to give you the best answer while making them feel like theyre better is a cool move

nuoza
Автор

Any string with 26 distinct characters will return true. This is not a correct solution. Checking `set(sentence) == would make it correct in all cases, but this is actually quite an inefficient solution, as it does a load of un-necessary hashing.
Better to iterate through the string once, checking off each character found. Then check that all lower case alphabetical chars were found.

SephirothITM
Автор

Sooo how did google hit an absolute low

Allenz
Автор

I don't comment on youtube videos much, but i'm doing this because i thought i got better solution traverse from 'a' to 'z' and check the indexOf(char) ever time if we get indexOf value as -1 we can return false since that particular character is not present in the given sentence, if you iterate through all the characters return true . Time complexity o(1) since it you will traverse of mostly 26 times for any input, you can even check whether the size is less than 26 if it does we can directly return false in the first place

lazyboy
Автор

I tried to do it when considering an arbitrarily long input string of 1000 characters (for example) where the pangram is a shorter subsequence toward the beginning of the input string. This algorithm is a little bit more efficient for those edge cases.

def checkIfPangram(self, sentence: str) -> bool:
chars = set()
length = 0
for i, c in enumerate(sentence):
if length == 26:
return True
if c not in chars:
chars.add(c)
length += 1

return length == 26

mufasum
Автор

I mean, it is labeled Easy. Could just be a simple data structures check. Follow up could ask what if other characters might be included, and even then a solution with a hash-set containing all lowercase letters removing each key as it appears and exiting when keys().length() is 0 is a very elementary solution.

dylanparrish-subda
Автор

I thought about making an empty array called alphabet and then go through the sentence with a for loop and check if the character is in the alphabet then check if array alphabet is 26 characters long

danielhod
Автор

i feel like using libraries or doing it in python kinda defeats the purpose

ianproctor
Автор

Create a list with all letters of the alphabet and a counter set on 26. Then start checking the letters of the string, everytime you find a letter of the list in the string, eliminate it from the list and subtract 1 from the counter. When the counter hits 0 you confirmed pangram, if you reach the end of the string with the counter greater 0 it is not a pangram.
Instead of the counter you could also check if the list is empty. But i like counters.

Lodrik_Bardric
Автор

when the sentence has a number in it 💀💀💀

jjophoven
Автор

It's not python, but...

func checkIfPangram(sentence string) bool {
if len(sentence) < 26 {
return false
}
var found uint32 = 0
var mask uint32 = (1 << ('z' + 1 - 'a')) - 1
for _, char := range sentence {
if char < 'a' || char > 'z' {
continue
}
found |= 1 << (char - 'a')
}
return found == mask
}

adambright
Автор

What's wrong with the question? Looks like a perfect warmup to me

hikari
Автор

String alphabet = "abc..."
for letter in alphabet{
if(!sentence.contains(letter) return false
}
return true

CoolExcite
Автор

A group of chars consisting of only lower case letters isn't a valid sentence :) That said, I probably would have done this:
def is_pangram(sentence):
if len(sentence) < 26:
return False

char_dict = {}
sentence_lower = sentence.lower()

for ch in sentence_lower:
if ch.isalpha():
char_dict[ch] = True

if len(char_dict) == 26:
return True

return False

jvanb
Автор

2*Run a for loop add a if with condition loop variable ascii value with present ascii in string position ascii or character if all 26 character are present boom else u know somewhat cool😂

Paper_Mastery
Автор

Question, wouldnt this catch spaces or do they not count as a character ? Also from the wording im unsure if punctuation would be in these sentance and again wouldnt these count as charectars ?

perrybird