0

I'm preparing for a job interview. So let's say I want to remove duplicate letters from a string, I came up with two functions to accomplish this:

# option 1:
def checkDuplicates(string):
    postCheck = ""
    previousChar = None
    for currentChar in string:
        if currentChar != previousChar:
            postCheck += currentChar
            previousChar = currentChar
    return postCheck.title()

# option 2:
def checkDuplicates2(string):
    scanned = set()
    postCheck = ""
    for currentChar in string:
        if currentChar not in scanned:
            scanned.add(currentChar)
            postCheck += currentChar
    return postCheck.title()

Testing:

>>> print(checkDuplicates("llaatteerr")
Later
>>> print(checkDuplicates2("llaatteerr")
Later

Now let's say I want to do the same thing but on a string of words where I have to loop over each word in a string. If there are duplicates in a word, remove the duplicate character. If the second word contains characters seen in the first, it doesn't matter - each word is its own case. So for instance:

>>> checkDup("llaatteerr tthhoouugghhtt")
Later Thought

So in order to do this, I created a second function:

def checkDupInWords(words):
    postCheck = ""
    for word in words.split():
        postCheck += f"{checkDuplicates(word)} "
    return postCheck

test:

>>>print(checkDupInWords("llaatteerr llaatteerr llaatteerr"))
Later Later Later

Now my question is, what if one word contained duplicate letters, and they are meaningful duplications (i.e. "Leonardo") Is this a task accomplishable and worth pursuing in short term? Or is it something that requires knowledge of linguistics and usage of external libraries?

Bonus question: would anyone say it's possible to encounter such task in an interview, or on a normal software developer job (I think it's possible if what you're working with is a project for language or something like that.) Any insight is appreciated!

o-az
  • 397
  • 2
  • 10
  • This is absolutely something you could encounter on an interview. Most of the time, these programming tests are not generally given because they have a practical use in and of themselves, but because they abstract a problem in Computer Science that does have great value in theory or in practice. So you generally wouldn't be given 'words' where containing duplicates would be meaningful, but clarifying the intent and parameters of a question is a useful skill that an interviewer might like to see. – Stephen Nov 12 '18 at 16:37
  • @Stephen that is precisely what I thought. The question of removing duplicates from a word is a common one, so I tried to extend it to removing duplicates from multiple words. Taking it further, I thought what if two letters occur in one word, but are not duplicates, like the example provided. – o-az Nov 12 '18 at 16:41
  • 1
    But the letter is still a duplicate in your example. Removing it would definetely remove the meaning of the word, but for correctness of the algorithm, it should remove those duplicate letters, even it means destroying the intended meaning of the actual word. This is where the actual requirements of your function come into play, as you would need to clarify those with the interviewer to figure out how you should handle such a case. – Stephen Nov 12 '18 at 16:46
  • @Stephen that makes sense. I must get into the habit of asking interviewer questions for clarification. – o-az Nov 12 '18 at 16:53

1 Answers1

0

Now my question is, what if one word contained duplicate letters, and they are meaningful duplications (i.e. "Leonardo")

My interpretation of duplicate letters would be a letter occurring twice or more in one word, and not necessarily adjacent.

Is this a task accomplishable and worth pursuing in short term?

The task itself is definitely doable and worth pursuing.

If you encounter a question like this, you should also perhaps try to clarify before building a solution in case how you interpret the task does not align with how the interviewer meant it. ps. i also do not know if this question really belongs here or not, its a strange one.

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
  • Where would the question belong if not here? I'm precisely interested in getting insight from regular Python users. I'm aware that in other languages - as well as in Python - there are some libraries which can be used to accomplish grammar check and spellcheck (i.e. grammar-check and language-check. – o-az Nov 12 '18 at 16:44