Oracle Interview Question for Software Engineer / Developers






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

// Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
// Complexity = O( n )   and space complexity O ( k ) where k is the length of the charset for ASCII  = 256
	public static boolean hasUniqueChars(char str []){
		boolean letters [] = new boolean[256];
		
		for(int i = 0; i < str.length ; i ++){
			if(str[i] == ' ') continue;
			char current = toUpperCase(str[i]);
			if(letters[current]){
				return false;
			}else{
				letters[current] = true;
			}
		}
		return true;
	}
	public static char toUpperCase(char c){
		if( c >= 'a' && c <= 'z'){
			c -= ('a' - 'A');
		}
		return c;
	}
	public static char toLowerCase(char c){
		if( c >= 'A' && c <= 'Z'){
			c += ('a'-'A');
		}
		return c;
	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would have same solution as you, but is array considered additional data structures?

- Leon January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

well. you can compare every character of the string to every other character of the string. This will give you O ( n square )

another option is, you can soft the array on O ( n long n ) and then, iterate to look for duplicates.
What about that?

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Makes sense, thanks.

- Leon January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

using memory of constant size if fine.

- nilukush June 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This wont work, because it would only find 'matched' characters if they happen to be at str[i] and str[n] - if the 2 identical characters are at str[i] and str[n-1], it would not find it? Did you try st = "ganegh";?

- Anonymous January 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does this work with definition??

public static void main(String[] args) {
        
                    
    
    String a="Shirish"; int y=0;
        for(int i=0;i<a.length() && y!=0;i++)
        {
            for(int j=i+1;j<a.length();j++)
            {
                if(a.charAt(i)!=a.charAt(j))
                    y=1;
                else {y=0;}
                               

            }
        }        
         if(y==1)
                System.out.print("no unique");
            else
                System.out.print(" unique"); 
    }
    
}

- Rahul Dhawani January 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Written in console application

string kelime = Console.ReadLine();
            bool varMi = true;
            for (int i = 1; i < kelime.Length; i++)
            {
                if (kelime[0]==kelime[i])
                {
                    varMi = false;
                    break;
                }
            }
            Console.WriteLine("is all characters unique: {0}", varMi);
            Console.Read();

- Anonymous January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the string O(nlogn).....traverse each char in string and check if next char == curr charr if yes break ....if no till u reach the end of the string return true .O(n);
total time complexity == O(nlogn)

- Danish Shaikh (danishshaikh556@gmail.com) February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the string O(nlogn).....traverse each char in string and check if next char == curr charr if yes break ....if no till u reach the end of the string return true .O(n);
total time complexity == O(nlogn)

- Danish Shaikh (danishshaikh556@gmail.com) February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the string O(nlogn).....traverse each char in string and check if next char == curr charr if yes break ....if no till u reach the end of the string return true .O(n);
total time complexity == O(nlogn)

- Danish Shaikh (danishshaikh556@gmail.com) February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the string O(nlogn).....traverse each char in string and check if next char == curr charr if yes break ....if no till u reach the end of the string return true .O(n);
total time complexity == O(nlogn)

- Danish Shaikh (danishshaikh556@gmail.com) February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the string O(nlogn).....traverse each char in string and check if next char == curr charr if yes break ....if no till u reach the end of the string return true .O(n);
total time complexity == O(nlogn)

- Danish Shaikh (danishshaikh556@gmail.com) February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

gadha kahi ka..

- wwee August 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

/* this one is the easy approach to do this, simply scan the string from both side if found any match character then its not unique. time complexity O(n/2) i.e. O(n). */

public class uniquestring {
public static boolean test_string(String str){
int n = str.length()-1;
int i=0;
while(i<n)
{
if(str.charAt(i++)== str.charAt(n--))
return true;
}
return false;
}
public static void main(String[] args){
String st;
st = "ganesh";
System.out.println(test_string(st));
}
}

- Ganesh1238 January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ganesh read the problem well before posting. Unique characters meas each one never is repeated in the string. No the other way around.

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This wont work, because it would only find 'matched' characters if they happen to be at str[i] and str[n] - if the 2 identical characters are at str[i] and str[n-1], it would not find it? Did you try st = "ganegh";?

- Anonymous January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gadha kahi ka...

- wwee August 17, 2014 | Flag


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