Interview Question for Software Engineers


Country: United States




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

#include <string>
#include <algorithm>

void sortSegments(std::string& input)
{
    bool toggle1 = isalpha(input[0])? true:false;
    size_t k = 0;
    for(size_t i = 1; i < input.size(); ++i)
    {
        bool toggle2 = isalpha(input[i])? true:false;
        if(toggle1 != toggle2)
        {
            std::sort(input.begin()+k, input.begin()+i);
            toggle1= toggle2;
            k=i;           
        }       
    }  
}

- steep September 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
void SortAlphaNumericString(string str)
{
string result="",temp;
int i=0;
for(;i<str.length();)
{
temp="";
if(str[i]>='A'&&str[i]<='Z')
{
while(i<str.length()&&str[i]>='A'&&str[i]<='Z')
{
temp=temp+str[i];
i++;
}
}
else
{
while(i<str.length()&&str[i]>='0'&&str[i]<='9')
{
temp=temp+str[i];
i++;
}
}
sort(temp.begin(),temp.end());
result=result+temp;
}
cout<<result<<"\n";

}
int main()
{
string str;
cin>>str;
SortAlphaNumericString( str);
return 0;
}

- Anonymous September 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.home.careercup;

import java.util.Arrays;

public class SortSegments {

    /*
    For example, the string "AZQF013452BAB" will result in "AFQZ012345ABB".
    The input letters will be uppercase and numbers will be between 0 and 9
    inclusive.
     */
    public static void main(String[] args) {
        final String text = "AZQF013452BAB";//"a1b1c1d312mmm22dgfjdfgjdg245ui4ye96";//
        StringBuilder sb  = new StringBuilder();
        for (int start = 0, end =breakIterator(text, start);
             end !=-1;
             start = end, end=end =breakIterator(text, start)) {
            char [] ss= text.substring(start,end).toCharArray();
            Arrays.sort(ss);
            sb.append(ss);

        }
        System.out.println(sb.toString());
    }

    /**
     * Launch search for next segment
     *
     * @param s search text string
     * @param start index where to start looking for the next segment
     * @return end index where the search for the segment ended
     */
    static int breakIterator(String s, int start) {
        if (start >= s.length()) return -1;
        boolean check = Character.isDigit(s.charAt(start));
        for (int i = start; i < s.length(); i++) {
            if (!(check == Character.isDigit(s.charAt(i)))) {
                return i;
            }
        }
        return s.length() ;
    }
}

- Makarand September 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void sortMerge(String data){

data="AZQF013452BAB";
StringBuilder builder=new StringBuilder();
StringBuilder finalBuilder=new StringBuilder();
ArrayList<String> list=new ArrayList<String>();
boolean flag=false;
for(int i=0;i<data.length();i++){
if(data.charAt(i)>=48 && data.charAt(i)<=57){
if(i!=0 && builder.charAt(builder.length()-1)>=65){

finalBuilder.append(sortData(builder.toString()));
builder=new StringBuilder();
}
builder.append(data.charAt(i));
}else if(data.charAt(i)>=65 && data.charAt(i)<=90){
if(i!=0 && builder.charAt(builder.length()-1)<65){
finalBuilder.append(sortData(builder.toString()));
builder=new StringBuilder();
}
builder.append(data.charAt(i));
}

}
finalBuilder.append(sortData(builder.toString()));
System.out.println(finalBuilder.toString());

}

- sandeep u September 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
public static void sortMerge(String data){

data="AZQF013452BAB";
StringBuilder builder=new StringBuilder();
StringBuilder finalBuilder=new StringBuilder();
ArrayList<String> list=new ArrayList<String>();
boolean flag=false;
for(int i=0;i<data.length();i++){
if(data.charAt(i)>=48 && data.charAt(i)<=57){
if(i!=0 && builder.charAt(builder.length()-1)>=65){

finalBuilder.append(sortData(builder.toString()));
builder=new StringBuilder();
}
builder.append(data.charAt(i));
}else if(data.charAt(i)>=65 && data.charAt(i)<=90){
if(i!=0 && builder.charAt(builder.length()-1)<65){
finalBuilder.append(sortData(builder.toString()));
builder=new StringBuilder();
}
builder.append(data.charAt(i));
}

}
finalBuilder.append(sortData(builder.toString()));
System.out.println(finalBuilder.toString());

}
}

- Anonymous September 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void swap(char* a,char* b)
{
char temp=*a;
*a=*b;
*b=temp;
}
int find(string &str,int l,int r)
{
char pivot=str[r];
int j=l;
int i=l;
while(i<r)
{
if(str[i]<pivot)
{
swap(&str[i],&str[j]);
j++;
}
i++;
}
swap(&str[j],&str[r]);
return j;
}
void sorting(string &str,int f,int l)
{
if(f<l)
{
int index=find(str,f,l);
sorting(str,f,index-1);
sorting(str,index+1,l);
}
}
void answer(string &str)
{
bool flag=false;
int l=str.length();
int i=0;
while(i<l)
{
int j=i;
while(i<l && str[i]>='A' && str[i]<='Z')
i++;
sorting(str,j,i-1);
j=i;
while(i<l && str[i]>='0' && str[i]<='9')
i++;
sorting(str,j,i-1);
}
cout<<str<<endl;
}
int main() {
string str="AZQF013452BBA";
answer(str);
return 0;
}

- Anonymous September 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SortSegmentsOfString {

public static void main(String[] args) {
// TODO Auto-generated method stub

SortSegmentsOfString ss = new SortSegmentsOfString();
String input = "AZQF013452BA432B8534AFCE";
System.out.println("Result string: "+ss.splitString(input));
}

public String splitString(String input)
{
List<String> sortedList = new ArrayList<String>();
boolean isAlpha = false, isNumeric = false;
int startIndex = 0;
for(int i=0;i<input.length();i++)
{
if(input.charAt(i) >='A' && input.charAt(i) <='Z')
{
if(!isAlpha && !isNumeric)
{
isAlpha = true;
}
else if (isNumeric)
{
isNumeric = false;
isAlpha = true;
sortedList.add(sortString(input.substring(startIndex, i)));
startIndex = i;
}

}
else if (input.charAt(i) >='0' && input.charAt(i) <='9')
{
if(!isAlpha && !isNumeric)
{
isNumeric = true;
}
else if (isAlpha)
{
isNumeric = true;
isAlpha = false;
sortedList.add(sortString(input.substring(startIndex, i)));
startIndex = i;
}

}
}
if(startIndex != input.length())
{
sortedList.add(sortString(input.substring(startIndex, input.length())));
}
StringBuilder sb = new StringBuilder();
for(String s: sortedList) {
sb.append(s);
}

return sb.toString();
}
public String sortString(String unsortStr)
{
char[] charArray = unsortStr.toCharArray();
Arrays.sort(charArray);
String sortedString = new String(charArray);

return sortedString;
}
}

- manteshsb September 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;

public class SortSegments {
	
	public static void main(String[] args) {
		String sample = "AZQF013452BAB";
		System.out.println("Sample string is: " + sample);
		System.out.println("Sorted Segments is: " + sortSegments(sample));
	}

	public static String sortSegments(String string) {
		StringBuilder sb = new StringBuilder();		
		char[] chars = string.toCharArray();
		int startIdx = 0;
		int switchIdx = 1;
		
		while (switchIdx < chars.length) {
			if (Character.isLetter(chars[startIdx]) && Character.isDigit(chars[switchIdx]) ||
				Character.isDigit(chars[startIdx]) && Character.isLetter(chars[switchIdx])) {
				char[] segment = Arrays.copyOfRange(chars, startIdx, switchIdx);
				Arrays.sort(segment);
				sb.append(segment);
				startIdx = switchIdx;
			}
			switchIdx++;
		}
		
		char[] finalSegment = Arrays.copyOfRange(chars, startIdx, switchIdx);
		Arrays.sort(finalSegment);
		sb.append(finalSegment);
		
		return sb.toString();
	}
}

- jasonk33 September 15, 2017 | 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