Comments on: Programming Interview Questions 15: First Non Repeated Character in String /2011/11/14/programming-interview-questions-15-first-non-repeated-character-in-string/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-15-first-non-repeated-character-in-string Information Retrieval and Machine Learning Mon, 23 Jan 2012 19:14:24 +0000 hourly 1 http://wordpress.org/?v=3.3 By: Kowshik /2011/11/14/programming-interview-questions-15-first-non-repeated-character-in-string/#comment-822 Kowshik Sun, 18 Dec 2011 09:36:33 +0000 /?p=787#comment-822 An optimization which a friend of mine pointed out to me: Instead of an array of pairs, we can use just use an array of integers, where -1 (default value) represents non-occurence of the character and -2 represents duplication. An optimization which a friend of mine pointed out to me: Instead of an array of pairs, we can use just use an array of integers, where -1 (default value) represents non-occurence of the character and -2 represents duplication.

]]>
By: Arden /2011/11/14/programming-interview-questions-15-first-non-repeated-character-in-string/#comment-807 Arden Sat, 17 Dec 2011 17:54:03 +0000 /?p=787#comment-807 You're totally right. I have a post ready to publish discussing exactly this solution. It'll be number 25: remove duplicate characters in string. Thanks for the comment.. You’re totally right. I have a post ready to publish discussing exactly this solution. It’ll be number 25: remove duplicate characters in string. Thanks for the comment..

]]>
By: Kowshik /2011/11/14/programming-interview-questions-15-first-non-repeated-character-in-string/#comment-801 Kowshik Sat, 17 Dec 2011 11:02:27 +0000 /?p=787#comment-801 Heres a twist to the same problem that I encountered in one of my recent interviews. What if the given string is extremely huge and the character set is lowercase letters ('a' to 'z')? Answer: Firstly, we can replace the hash table with an array of pairs of size 26 (each representing a lowercase letter): - The first element in each pair stores the location where the character occured for the first time in the string (default value is -1 indicating non-occurence). - The second element in each pair is a boolean that indicates if the character's occurence is unique. (default value is false and it also indicates repetition. true indicates uniqueness.) With this data structure ready, it is enough if we parse through the original string only once to populate the array of pairs. Then we do a scan on the array to figure out the first unique character. Heres a twist to the same problem that I encountered in one of my recent interviews. What if the given string is extremely huge and the character set is lowercase letters (‘a’ to ‘z’)?

Answer: Firstly, we can replace the hash table with an array of pairs of size 26 (each representing a lowercase letter):
– The first element in each pair stores the location where the character occured for the first time in the string (default value is -1 indicating non-occurence).
– The second element in each pair is a boolean that indicates if the character’s occurence is unique. (default value is false and it also indicates repetition. true indicates uniqueness.)

With this data structure ready, it is enough if we parse through the original string only once to populate the array of pairs. Then we do a scan on the array to figure out the first unique character.

]]>
By: Arden /2011/11/14/programming-interview-questions-15-first-non-repeated-character-in-string/#comment-505 Arden Mon, 14 Nov 2011 20:48:30 +0000 /?p=787#comment-505 Good alternative solution. But if we replace the binary search tree with hashtable, updating the counts will be slightly faster I guess? Good alternative solution. But if we replace the binary search tree with hashtable, updating the counts will be slightly faster I guess?

]]>
By: alp /2011/11/14/programming-interview-questions-15-first-non-repeated-character-in-string/#comment-503 alp Mon, 14 Nov 2011 20:21:13 +0000 /?p=787#comment-503 I think we can traverse the string only once by keeping two trees, one is just a binary search tree to keep nodes of characters and their counts, one is a minheap to keep those nodes sorted and while traversing we'll maintain the heap and then after the traversal is finished, answer is at the root of the heap. That comes with a memory paste and we can maintain the heap in O(N) time, I think. That's not a better solution, just wanted to leave it here. I think we can traverse the string only once by keeping two trees, one is just a binary search tree to keep nodes of characters and their counts, one is a minheap to keep those nodes sorted and while traversing we’ll maintain the heap and then after the traversal is finished, answer is at the root of the heap. That comes with a memory paste and we can maintain the heap in O(N) time, I think. That’s not a better solution, just wanted to leave it here.

]]>