Directi Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

Simple O(n) solution with traversal of string.
Keep track of number of characters found so far, if it exceeds or equals k then print the corresponding previously saved character.

char func_kth_char( string s, int k )
{
	int i, l, fc, j, n;
	l = s.length();
	
	fc = j = 0;
	
	for( i = 1; i < l; j = i++ ) {
		n = 0;
		while( i < l && s[i] >= '0' && s[i] <= '9' ) {
			n = n*10 + (s[i++]-'0');
		}
		fc += n;
		
		if( fc >= k ) {
			return s[j];
		}
	}
}

- Cerberuz September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a3b2c2 = (aaab)2c2 = (aabaabc)2 = aabaabcaabaabc looks not correct.
if a3b2c2 = (aaab)2c2 = (aaabaaabc)2 = aaabaaabcaaabaaabc then

char find(String s, int k){
		int len=0;
		int i;
		String result = "";
		
		for(i=1; i<s.length(); i++){			
			String l = getInt(i,s);
			len = Integer.parseInt(l);
			String seg = result + s.charAt(i-1);
			result = "";
			for(int j=0; j<len; j++)result += seg;			
			if(k < len) break;
			i += l.length();
		}
		return result.charAt(k-1);
	}
	String getInt(int start, String s){
		String i="";
		while(start<s.length() && s.charAt(start)>='0'&&s.charAt(start)<='9'){
			i += s.charAt(start);
			start++;
		}
		return i;
	}

- jose September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I dont think there is a need to expand the string in the first place. Given string is a3b2c2. Suppose k is 5. We can subtract the numbers one after other from k, and when k becomes <=0, the character to the left is the letter.

So k=5. 5-3 (First number in the string) = 2;
2-2 (next number in the string) 0. Therefore, the character is 'b'. If k is still positive and we reach the end of the string, we return Null.

- Vijay September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the input string is="ab3c2d2" and k=5,
then according to your solution answer should be 'c'.But here answer is a.

- anshrox September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it works for a input like a300b200c123

#include<stdio.h>
int main()
  {
     char *s,ch;
     int k,i;
     printf("please enter a string");
     gets(s);
     printf("Enter k");
     scanf("%d",&k);
     while(k>0)
      {

	if((int)*s>=48 && (int)*s<=57)
	  {
	    while((int)(*s)<58)
	      {
		i=i*10+ (int)*s-48;
		s++;
	       }
	     k=k-i;


	     }
	     else
	      ch=*s;
	   s++;
	}
       printf("%c",ch);
      return 0;
    }

- aakash September 20, 2012 | 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