Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: Written Test




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

{
static void Main(string[] args)
{
string inString = "Listen my children and you shall hear of the midnite-ride of Paul Revere, 1 if by land and 2 if by sea, the redcoats are coming!";
string[] Words = inString.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);

List<string> Outwords = new List<string>();

int BgnPos = 0;
int EndPos = Words.Length;


while (BgnPos < EndPos)
{
string AWord = Words[BgnPos];

if (AWord.Length < 3)
{
Outwords.Add(AWord);
}
else
{
string AbvBgn = string.Empty;
int l = AWord.Length;
bool hasFirst = false;

for (int i = 0; i < l; i++)
{
if (Char.IsLetter(AWord, i))
{
if (!hasFirst)
{
AbvBgn = AWord.Substring(0, i + 1).ToUpper();
hasFirst = true;
break;
}
}
}

string AbvEnd = string.Empty;
for (int i = l-1; i > 0; --i)
{
if (Char.IsLetterOrDigit(AWord, i))
{
AbvEnd = AWord.Substring(i, l - i).ToLower();
break;
}
}

int Cntr = 0;
string AbvMid = string.Empty;
for (int i = AbvBgn.Length; i < l - AbvEnd.Length; i++)
{
if (Char.IsLetterOrDigit(AWord, i))
{
Cntr++;
}
else
{
AbvMid = AbvMid + Cntr.ToString() + AWord[i];
Cntr = 0;
}
}
if (Cntr > 0)
{
AbvMid = AbvMid + Cntr.ToString();
}

string NewWord = AbvBgn + AbvMid + AbvEnd;
Outwords.Add(NewWord);
}
BgnPos++;
}

foreach (string Word in Outwords)
{
Console.WriteLine(Word);
}
string Oprkeys = Console.ReadKey().ToString();
}
}

- Woody November 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Woody,

Thanks. But I have couple of questions:

1. if there is a word "1234" then looks like you will completely skip it, coz u r checking for isLetter in abvBg and abvEnd

2. the non alphabetic characters in string should stay as it is. I see you r skipping them all together.

- Dee November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you check i am leaving the non letter characters in place, and all punctuation stays in place also. so if the input was freddy-mercury i output F5-6y, just as if the input was boston. i output B4n.

- Woody November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(
String[] args) {
String sentence=args[0];
String[] arr = sentence.split(" ");
String abbr;
char first;
char last;
StringBuffer sb =new StringBuffer();
for (String word:arr) {
first=word.charAt(0);
last=word.charAt(word.length()-1);
abbr=first+String.valueOf(word.length()-2)+last;
sb.append(abbr).append(" ");
}
System.out.println(sb);
}

- nirupam.astro November 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are a few questions that need to be clarified:

1. If a word has numerals in it, since we need to leave it as is, what representation is needed for them. E.g. Global45 - what is the expected conversion?
2. What is expected if a word has number substring? E.g. Carl4Mayor.
3. If there is only a number, is that to be considered a word? E.g. 1234

Only when we understand these requirements, can we assess the solution (extra memory needed or not etc.)

- NewCoderInTown November 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am making this post to be one of the early ones but I will edit it heavily with my solution.
Then I will make more posts and visualizations in other posts.

Please give me plus votes.

- Ajeet November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stop spamming Ajeet. I hate spammers and it is my job to keep them away. You sound like a _troll_ and a _dumbfuck_. If so, let it be known that I am smarter than thou also because this question is easy!

- urik on bb November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class Word {
	
	private String sentence="mera naam deven hai";
	private ArrayList<String> str= new ArrayList<String>();
	
	public void tokens(String sentence)
	{
		char ch;
		String temp="";
		int len=0;
		for(char c: sentence.toCharArray())
		{
			if(len==(sentence.length()-1))
			{
				temp+=c;
				str.add(temp);
			}
			else if(c==' ')
			{
				
				str.add(temp);
				temp="";
				
			}
			else
			{
				temp+=c;
			}
			len++;
		}
		
		//return str;
	}

	public void initials()
	{
		
	}
	public static void main(String[] args)
	{
		Word w = new Word();
		w.tokens(w.sentence);

		char first,last;
		int len;
		String result="";
		for(Object s: w.str.toArray())
		{
			String a=(String)s;
			//System.out.println(a);
			if(a.length()<=2)
			{
				System.out.print(a + " ");
			}
			else
			{
				len=a.length()-2;
				result+=a.charAt(0);
				result+=len;
				result+=a.charAt(a.length()-1);
				System.out.print(result+" ");
				result="";
			}
			//System.out.println(s.toString());
		}
		
	}
}

- Deven Kalra November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String processSentence(String sentence){
		StringBuilder  finalSentenceChars = new StringBuilder();
		if(sentence == null){
			return "";
		}
		char[] characters = sentence.toCharArray();
		int position = 0;
		ArrayList<Character> word = new ArrayList<Character>();
		while (position != sentence.length()){
			if(!Character.isAlphabetic(characters[position])){
				if(!word.isEmpty()){
					finalSentenceChars.append(transform(word));
					word = new ArrayList<Character>();
				}
				finalSentenceChars.append(characters[position++]);
			}else{
				word.add(characters[position++]);
			}
		}
		finalSentenceChars.append(transform(word));
		return finalSentenceChars.toString();
	}

	private String transform(ArrayList<Character> word) {
		return String.format("%c%d%c",word.get(0),(word.size() - 2), word.get(word.size() -1));
	}

- mad November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * Programming Language used :  PHP
 * This method abbreviates the words in the $sentence.
 * It would affect only words with alphabets only.
 * e.g. "Human" will become "H4n". All non-alphabetic characters will be
 * kept as it is.
 * 
 * @author Varun Jalandery <varun.jalandery@gmail.com>
 * 
 * @param string $sentence
 * @return string abbreviated sentence
 */
function abbreviateSentence($sentence) {
    $result = '';
    $length = strlen($sentence);
    $isWordStarted = false;
    $wordInnerLength = 0;

    for ($i = 0; $i < $length; $i++) {
        if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
            if ($isWordStarted) {
                $wordInnerLength += 1;
            } else {
                $isWordStarted = true;
                $result .= $sentence[$i];
            }
        } else {
            if ($isWordStarted) {
                $result .= (string) ($wordInnerLength - 1) . $sentence[$i - 1] . $sentence[$i];
                $wordInnerLength = 0;
                $isWordStarted = false;
            } else {
                $result .= $sentence[$i];
            }
        }
    }
    return $result;

}

- Varun Jalandery November 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/**
 * Programming Language used :  PHP
 * This method abbreviates the words in the $sentence.
 * It would affect only words with alphabets only.
 * e.g. "Human" will become "H3n". All non-alphabetic characters will be
 * kept as it is.
 * 
 * @author Varun Jalandery <varun.jalandery@gmail.com>
 * 
 * @param string $sentence
 * @return string abbreviated sentence
 */
function abbreviateSentence($sentence)
{
    $result = '';
    $length = strlen($sentence);
    $isWordStarted = false;
    $wordInnerLength = 0;

    for ($i = 0; $i < $length - 1; $i++) {
        if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
            if ($isWordStarted) {
                $wordInnerLength += 1;
            } else {
                $isWordStarted = true;
                $result .= $sentence[$i];
            }
        } else {
            if ($isWordStarted) {
                if ($wordInnerLength - 1 > 0) {
                    $result .= (string) ($wordInnerLength - 1);
                }
                $result .= $sentence[$i - 1] . $sentence[$i];
                $wordInnerLength = 0;
                $isWordStarted = false;
            } else {
                $result .= $sentence[$i];
            }
        }
    }
    
    if ($isWordStarted) {
        if ($wordInnerLength > 0) {
            $result .= (string) ($wordInnerLength);
        }
    }
    
    $result .= $sentence[$i];
    
    return $result;

}

- Varun Jalandery November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is one mistake in the comments section. "Human" would become H3n and not H4n.

- Varun Jalandery November 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/**
 * Programming Language used :  PHP
 * This method abbreviates the words in the $sentence.
 * It would affect only words with alphabets only.
 * e.g. "Human" will become "H3n". All non-alphabetic characters will be
 * kept as it is.
 * 
 * @author Varun Jalandery <varun.jalandery@gmail.com>
 * 
 * @param string $sentence
 * @return string abbreviated sentence
 */
function abbreviateSentence($sentence)
{
    $result = '';
    $length = strlen($sentence);
    $isWordStarted = false;
    $wordInnerLength = 0;

    for ($i = 0; $i < $length - 1; $i++) {
        if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
            if ($isWordStarted) {
                $wordInnerLength += 1;
            } else {
                $isWordStarted = true;
                $result .= $sentence[$i];
            }
        } else {
            if ($isWordStarted) {
                if ($wordInnerLength - 1 > 0) {
                    $result .= (string) ($wordInnerLength - 1);
                }
                $result .= $sentence[$i - 1] . $sentence[$i];
                $wordInnerLength = 0;
                $isWordStarted = false;
            } else {
                $result .= $sentence[$i];
            }
        }
    }

    //handled the boundary case separately, otherwise for every iteration it would 
    //be required to test whether its a boundary case or not.
    if (preg_match('/^[A-Za-z]$/', $sentence[$length - 1])) {
        if ($isWordStarted) {
            if ($wordInnerLength > 0) {
                $result .= (string) ($wordInnerLength);
            }
        }
    } else {
        if ($isWordStarted) {
            if ($wordInnerLength - 1 > 0) {
                $result .= (string) ($wordInnerLength - 1) . $sentence[$length - 2];
            }
        }
    }

    $result .= $sentence[$length - 1];

    return $result;
}

- Varun Jalandery November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String Abbreviate(String sentence){
		char [] chs =sentence.toCharArray();
		boolean prehasSpace=true;
		int charCount = 0;
		int j =0, i =0 ;
		for ( ; i < chs.length ; ++i){
			if (Character.isAlphabetic(chs[i])){
				//find the start of the word
			   if (prehasSpace){
					prehasSpace=false;
					chs[j++]=chs[i];
				}else{
					charCount++;	
				}		   
			}else{
				prehasSpace=true;
				if (Character.isAlphabetic(chs[i-1])){
					chs[j++]=(char)((charCount-1)+'0'); 
					chs[j++]=chs[i-1];
					charCount=0;	
				}
				chs[j++]=chs[i];	
			}	
		}
		if (Character.isAlphabetic(chs[i-1])){
			chs[j++]=(char)((charCount-1)+'0');
			chs[j++]=chs[i-1];
		}
		return new String(chs,0,j);	
	}

- Scott November 24, 2013 | 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