Sapient Corporation Interview Question Developer Program Engineers

  • sapient-corporation-interview-questions
    0
    of 0 votes
    11
    Answers

    write a program to print the given string as alphabets in order next integres with sum next special characters

    example: CAE2W3@D# as input and output should be

    ACDEW5@#

    - kiran on February 29, 2012 in India Report Duplicate | Flag
    Sapient Corporation Developer Program Engineer Arrays

Country: India
Interview Type: In-Person


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

1) seperate alphabet /number/others
like dutch flags problem
2) sort alphabets part

- anonymous on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void partition(int a[], int n)
{
int low==0,mid=0,high=n-1;
while(mid<high)
{
switch(a[mid])
{
case 0:
swap(&a[low++],a[mid]);
break;
case 1:
mid++;
break;
case 2:
swap(&a[high--],&a[mid]);
break;
}
}
}

- Siva on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i would have 3 empty strings at the beginning one for storing letters, one for numbers and one for symbols.

then loop on the input string, append each character to its corresponding string. then sort each of the 3 strings. and concatenate them in the required order.

it will be in O(n log n) time and O(n) space

- hitman on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since when they started asking such questions. Did you tag the company name correctly?

- nihaldps on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use hashing for the alphabets then it will be a O(n) algo simply

- codinglearner on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.
i) Create a char array of size 26 for hashing the occurences of the letters.
ii) Create an array ( depends upon the number of special characters ) for storing the special
characters. ( I assume that there is no sorted order for them, though even if there was one, it
could be handled ).
iii) A temporary int variable would hold the sum.

2. Traverse the given array.
i) Hash the occurences of the alphabets by maintaining a count.
ii) Append the special characters to the array allocated for them.
iii) Keep updating the sum as and when you encounter a number.

3. Printing remains.
i) Traverse the alphabet array in order printing the occurences of the letters.
ii) Print the sum.
iii) The special character string may now be printed as well.

O(N) time and constant space.

- ghantacoder on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.
i) Create a char array of size 26 for hashing the occurences of the letters.
ii) Create an array ( depends upon the number of special characters ) for storing the special
characters. ( I assume that there is no sorted order for them, though even if there was one, it
could be handled ).
iii) A temporary int variable would hold the sum.

2. Traverse the given array.
i) Hash the occurences of the alphabets by maintaining a count.
ii) Append the special characters to the array allocated for them.
iii) Keep updating the sum as and when you encounter a number.

3. Printing remains.
i) Traverse the alphabet array in order printing the occurences of the letters.
ii) Print the sum.
iii) The special character string may now be printed as well.

O(N) time and constant space.

- Anonymous on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain in detail by simple program

- kiran on March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

implementation in Java

public class TidyCharacters {

	public static String tidy(String s){
		int[] lettersFrequency = new int[26];
		int digitsSum = 0;
		StringBuilder specialChars = new StringBuilder();
		for(char c:s.toCharArray()){
			if( '0' <= c && c <= '9' ){
				digitsSum += ( c - '0' );
			} else if ( 'A' <= c && c <= 'Z' ){
				lettersFrequency[c - 'A']++;
			} else {
				specialChars.append(c);
			}
		}
		StringBuilder result = new StringBuilder();
		for( int i=0;i<lettersFrequency.length;i++){
			int count = lettersFrequency[i];
			for( int j=0;j<count;j++){
				result.append((char)('A'+i));
			}
		}

		result.append(digitsSum);
		result.append(specialChars);
		return result.toString();
	}

	public static void main(String[] args){
		System.out.println("<< "+args[0]);
		System.out.println(">> "+tidy(args[0]));
	}
}

- syl20j on June 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Scala version

def printSpecial(s:String)= {
    val charRange = ('a' to 'z') ++ ('A' to 'Z')
    val numberRange = ('0' to '9')
    def _group(todo:List[Char], chars:List[Char], specialChars:List[Char], sum:Int, fOnFinish:(List[Char], List[Char], Int)=>Unit):Unit = {
      todo match {
        case head::tail=>
          if (charRange contains head) {
            _group(tail, chars :+ head, specialChars, sum, fOnFinish)
          } else if (numberRange contains head) {
            _group(tail, chars, specialChars, sum + head.asDigit, fOnFinish)
          } else {
            _group(tail, chars, specialChars :+ head, sum, fOnFinish)
          }
        case _ => fOnFinish(chars,specialChars,sum)
      }
    }
    _group(s.toList, Nil, Nil, 0, (chars, special, sum) => {
        print(chars.sortBy(_.asDigit).foldLeft("")(_+_))
        print(sum)
        println(special.foldLeft("")(_+_))
      })
  }

- IvanoBulo on February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;

public class Example {
/*public static void main(String... args) {
String pratp="";

pratp.getChars(0,pratp.length(), char[] dst, 0);
System.out.println();
//for(){
//System.out.println(x + " " + y + " ");
}*/
public static void main(String[] args){
String str = "CAE2W3@D#";
int sum=0;
char[] arr = new char[str.length()];
char[] arr1 = new char[str.length()];
char[] arr2 = new char[str.length()];
str.getChars(0, str.length(), arr, 0);
System.out.print("The Character array equals: ");
System.out.println(arr);
int st1 = 0;
int st2 = 0;
for(int i=0;i<arr.length;i++){
char temp=arr[i];
if(Character.isLetter(temp)){
System.out.println(temp +"is Letter on index" +i);
arr1[st1]=temp;
st1++;
//st1.append(arr[i]);
}
else if(Character.isDigit(temp)){
System.out.println(arr[i] +"is digit on index" +i);
int n=Integer.valueOf(temp+"");
System.out.println("int value is :"+n);
sum=sum+n;
}
else{
System.out.println(temp] +"is SPL Char on index" +i);
//arr2.add(arr[i]);
arr2[st2]=temp;
st2++;
//st2.append(arr[i]);
}
}
Arrays.sort(arr1);
System.out.print(arr1);
System.out.print(sum);
System.out.print(arr2);
}
}

- Pratap G B on August 21, 2012 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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