11. Find Duplicate Characters Count in a String in C# | C# Interview Questions

11. Find Duplicate Characters Count in a String in C# | C# Interview Questions

Krishna Sakinala

4 года назад

10,778 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Aaron Carter
Aaron Carter - 20.11.2022 14:22

You're definitely gonna fail a real code interview with this, lol. You made it unnecessarily slow and verbose.

The first big mistake you make is using str.ToCharArray() right at the beginning. The interviewer is going to realize right then that the person doesn't understand how memory and the stack vs managed heap works and clearly doesn't realize they're making an unnecessary copy of the data before they even begin trying to solve the problem. In the real world the input strings could be something like millions and millions of chars worth of social media or forum posts, and you would probably also have a harder task like finding multiple words and identifying patterns rather than counting one char, but these simpler problems are sometimes used at the beginning in interviews to weed out people who don't have the necessary skills, and the real problems that come after will still be simpler than the ones at a job just so the interview doesn't take a week to finish lol.

But these are the kind of "red flags" the interviewer is looking out for at the beginning and they might tell you right then to stop, ask you a couple questions to see if you realize the huge mistake you made and if not they might just end the interview then and tell you try again someday ... this huge mistake basically lets them know, right off the bat, that the candidate probably isn't ready for the complexity of the real job and working in a production environment. So it would probably be the end of you interview, unless they are really polite and nice ...

Note that calling ToCharArray not only makes a completely unneeded copy of the input data that wastes memory (which could be something like 2.6GB+ of text in real life) but it's also causing an iteration over the data to do it, which you have to assume could be up to an O(n1) complexity ... so it instantly doubled your memory usage and you've already wasted a whole traversal over the data at the start for nothing.

The code is also needlessly verbose, like using num = num + 1 instead of just putting ++num to increment.

All you need to do is a plain for loop and index the char inside the the string like:

if( dict.ContainsKey( str[ i ] ) )
++dict[ str[ i] ];
else dict.Add( str[ i ], 1 );

Simpler, shorter and faster, no doubling your memory use and spending an iteration over the data set for nothing. If the string is smaller and you're looking for the count of a smaller number of chars within a string, then the for loop is really fast. If you've got a huge amount of text to go over and need to count the number of times many different chars appear then it becomes a bottleneck because it's O(n1) and n is a very big number ... if we have to count a very big number of chars in a big string, that means it takes a long, long time to do them one at a time. In that case, we could probably do it faster by using a Parallel.For( ... ) and a ConcurrentDictionary so we can count several chars along the big string at once and cut the time by a lot. If you wanna make it even faster, then use a fixed( char* p = str ) to fix the string in memory and then iterate the data with the raw pointer to eliminate runtime bounds checks and other safety features that slow the loop down. That will make it about the same as C++ performance, in theory.

This one is honestly a bit too easy to be a serious interview question for a real job though. You should re-do this and find one of the more realistic string-related problems on an interview prep site like Hackerrank, Codility or Leetcode ... theirs will ask you do things like find out how many times you can find the different string patterns in the string[] array "patterns" inside of an input of string or string[] called "inputs", and introduce some more challenging ideas like you'd see at a real-life job. If all you had to do for a programming job was count chars in a loop then we wouldn't have anyone to collect the garbage or sweep the floors, lol, everyone would be a programmer. So work on more realistic interview problems and improving your code and understanding of logic, memory, etc.

Ответить
Arun Ravindran
Arun Ravindran - 18.06.2020 07:28

you don't need hashset for this. you can easily loop dictionary and print

Ответить
Mehrdad Aghamolaei
Mehrdad Aghamolaei - 14.04.2020 19:44

At the end, you can easily iterate over dictionary keys, not needed to define HashSet

Ответить
Mahesh Konni
Mahesh Konni - 12.02.2020 22:02

Great job sir...this question was asked in interview to me...can i get your WhatsApp number sir

Ответить