Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do it by creating an index array of size 26(assuming string contains only English lower alphabet characters)..
Visit the string once and do +1 for each character in that array.
Now visit the string again from left to right and check in the array if number in array is one, you have found your first non-repeating character.

Code;

str = "abcbae"

arr = [0]*26

for ch in str:
	arr[ord(ch)-97] +=1

result_index = -1
current_index = 0
for ch in str:
	if arr[ord(ch)-97]==1:
		result_index = current_index
		break
	current_index +=1
print(result_index)

- jatinkhurana9 November 27, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use linkedhashMap where {k,v} k => A character , v => frequency. LinkedHashMap maintains the order of insertion.

- leetcoder December 13, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Setup an array[26] (initialized to all Zeroes) of following struct, corresponding to each alphabet.

struct {
	int count;
	struct Node* entryPtr;
    };

- create a head ptr of struct Node, which will grow as and when a NEW char is seen, and diminishes when a char in it gets repeated.

Algo:
As soon as a letter is read, check in the array for the alphabet-count.
1. If the current count is <1 then create a Node with the alphabet and append the char into the array, AND save its Node's address at the corresponding location in the array while incrementing the count.
2. If the current count is =1 (which means the alphabet is seen >1) then, increment the count AND delete the Node pointed by the Node ptr present at the array index.
3. At any time the head of the array would point to the Node which contains the latest NON-repeating char.

- anon December 24, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Setup an array[26] (initialized to all Zeroes) of following struct, corresponding to each alphabet.

struct {
	int count;
	struct Node* entryPtr;
    };

- create a head ptr of struct Node, which will grow as and when a NEW char is seen, and diminishes when a char in it gets repeated.

Algo:
As soon as a letter is read, check in the array for the alphabet-count.
1. If the current count is <1 then create a Node with the alphabet and append the char into the array, AND save its Node's address at the corresponding location in the array while incrementing the count.
2. If the current count is =1 (which means the alphabet is seen >1) then, increment the count AND delete the Node pointed by the Node ptr present at the array index.
3. At any time the head of the array would point to the Node which contains the latest NON-repeating char.

- Anon December 24, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Setup an array[26] (initialized to all Zeroes) of following struct, corresponding to each alphabet.

struct {
	int count;
	struct Node* entryPtr;
    };

- create a head ptr of struct Node, which will grow as and when a NEW char is seen, and diminishes when a char in it gets repeated.

Algo:
As soon as a letter is read, check in the array for the alphabet-count.
1. If the current count is <1 then create a Node with the alphabet and append the char into the array, AND save its Node's address at the corresponding location in the array while incrementing the count.
2. If the current count is =1 (which means the alphabet is seen >1) then, increment the count AND delete the Node pointed by the Node ptr present at the array index.
3. At any time the head of the array would point to the Node which contains the latest NON-repeating char.

- anon December 24, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import OrderedDict

def get_first_non_repeated_char(input_string):
'''
@param: input_string: str, input_string
@return: output_string: str, first non-repeated character
'''
order_dict = OrderedDict()
tmp_list = [(char,1) for char in input_string]

for key, value in tmp_list:
if key in order_dict.keys():
order_dict[key] = order_dict[key] + value
else:
order_dict[key] = value

result=[]
for key, value in order_dict.items():
if value == 1:
result.append(key)

return result[0]

- Python Implementation by OrderedDict, Tested December 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import OrderedDict 

def get_first_non_repeated_char(input_string):
    '''
    @param: input_string: str, input_string
    @return: output_string: str, first non-repeated character
    '''
    order_dict = OrderedDict()
    tmp_list = [(char,1) for char in input_string]
    
    for key, value in tmp_list:
        if key in order_dict.keys():
            order_dict[key] = order_dict[key] + value
        else: 
            order_dict[key] = value
    
    result=[]
    for key, value in order_dict.items():
        if value == 1:
            result.append(key)

    return result[0]

- Python Implementation by OrderedDict, Tested December 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import OrderedDict 

def get_first_non_repeated_char(input_string):
    '''
    @param: input_string: str, input_string
    @return: output_string: str, first non-repeated character
    '''
    order_dict = OrderedDict()
    tmp_list = [(char,1) for char in input_string]
    
    for key, value in tmp_list:
        if key in order_dict.keys():
            order_dict[key] = order_dict[key] + value
        else: 
            order_dict[key] = value
    
    result=[]
    for key, value in order_dict.items():
        if value == 1:
            result.append(key)

    return result[0]

- Python Implementation by OrderedDict, Tested December 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import OrderedDict

- Anonymous December 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import OrderedDict 

def get_first_non_repeated_char(input_string):
    '''
    @param: input_string: str, input_string
    @return: output_string: str, first non-repeated character
    '''
    order_dict = OrderedDict()
    tmp_list = [(char,1) for char in input_string]
    
    for key, value in tmp_list:
        if key in order_dict.keys():
            order_dict[key] = order_dict[key] + value
        else: 
            order_dict[key] = value
    
    result=[]
    for key, value in order_dict.items():
        if value == 1:
            result.append(key)

    return result[0]

- Tested Python implementation with OrderedDict December 29, 2020 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More