Uber Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

this is simple with following steps
1. lets begin with position i=0
2. jump to i+25
3. from position i+25 go back unless you get a whitespace... lets say you got whitespace at i+23.
4. send sms from i to i+23 and then set i = i+24 and repeat from step 2
5. follow these steps unless you are out of boudary in step 2.

- vikas maan November 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++, implementation

vector<string> splitWord(string str, int limit) {
    vector<string> arr;
    vector<int> marks;
    vector<int> spaces;
    string size;
    int i, start, markIdx, spaceIdx;
    
    for (i = 0; i < str.size(); i++) {
        if (str[i] == ' ') {
            spaces.push_back(i);
            if (i == 0) continue;
            switch (str[i - 1]) {
                case ',': case '.': case '?': case '!':
                    marks.push_back(i);
            }
        }
    }
    spaces.push_back(i);
    marks.push_back(i);
    
    start = 0;
    markIdx = 0;
    spaceIdx = 0;
    while (markIdx < marks.size()) {
        if (marks[ markIdx ] - start <= limit) {
            markIdx++;
            continue;
        }
        
        if (markIdx > 0 && marks[ markIdx - 1 ] > start && marks[ markIdx - 1 ] - start <= limit) {
            arr.push_back(str.substr(start, marks[ markIdx - 1 ] - start));
            start = marks[ markIdx - 1 ] + 1;
        } else {
            while (spaceIdx < spaces.size() && spaces[ spaceIdx ] <= start + limit) spaceIdx++;
            if (spaceIdx > 0 && spaces[ spaceIdx ] > start + limit) spaceIdx--;
            if (spaces[ spaceIdx ] > start && spaces[ spaceIdx ] <= start + limit) {
                arr.push_back(str.substr(start, spaces[ spaceIdx ] - start));
                start = spaces[ spaceIdx ] + 1;
            } else {
                arr.push_back(str.substr(start, limit));
                start += limit;
            }
        }
    }
    arr.push_back(str.substr(start, marks[ markIdx - 1 ] - start));
    
    size = "/" + integerToString((int)arr.size()) + ")";
    for (i = 0; i < arr.size(); i++) {
        arr[i] += "(" + integerToString(i + 1) + size;
    }
    
    return arr;
}

- kyduke November 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Hi Sivasrinivas, your Uber is arriving now!", 25 => ["Hi Sivasrinivas,(1/3)", "your Uber is arriving(2/3)", "now!(3/3)"]

"01234567890123456789", 15 => ["012345678901234(1/2)", "56789(2/2)"]

utility functions

void printStringArray(vector<string> arr) {
    int i;
    
    cout << "[";
    for (i = 0; i < arr.size(); i++) {
        cout << "\"" << arr[i] << "\"";
        if (i != arr.size() - 1) cout << ", ";
    }
    cout << "]\n";
}

string integerToString(int n) {
    string str;
    string table[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    
    if (n == 0) return "0";
    
    str = "";
    while (n) {
        str = table[n % 10] + str;
        n /= 10;
    }
    
    return str;
}

- kyduke November 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is simple with following steps
1. lets begin with position i=0
2. jump to i+25
3. from position i+25 go back unless you get a whitespace... lets say you got whitespace at i+23.
4. send sms from i to i+23 and then set i = i+24 and repeat from step 2
5. follow these steps unless you are out of boudary in step 2.

- vikas maan November 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is simple with following steps
1. lets begin with position i=0
2. jump to i+25 
3. from position i+25 go back unless you get a whitespace... lets say you got whitespace at i+23.

- vikas maan November 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You're forgetting you need to leave space for the (1/3) as well as accounting for the current split count.

- famish99 November 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Also, what if there is no space before i+25

- NewGuy November 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Steps:
1. Split the String by " "
2. Go through the array and keep adding the elements to a new String until the length is less than charLimit

import java.util.ArrayList;

public class SplitTextDemo {

    static ArrayList<String> splitText(String message, int charLimit) {

        return splitTextAuxUsingSplit(message, charLimit);
    }

    static ArrayList<String> splitTextAuxUsingSplit(String message, int charLimitOriginal) {
        //Decrease the char limit to accomodate chunk number at the end i.e. (1/3). For now assuming, the message chunks won't be more than 9
        int charLimit = charLimitOriginal - 5;
        //New arraylist to store message chunks
        ArrayList<String> result = new ArrayList<String>();
        String[] splitted = message.split(" ");
        String temp;

        for (int i = 0; i < splitted.length - 1; i++) {
            temp = splitted[i];
            //while length of temp and the next element combined is less than charLimit, temp = temp + next element
            //Last element to be taken care of after this loop
            while ((temp + 1 + splitted[i + 1]).length() <= charLimit && i + 1 < splitted.length - 1) {  //+1 for space
                temp = temp + " " + splitted[i + 1];
                i++;
            }
            result.add(temp);

        }
        //Take care of the last element
        //Add the last element from splitted to the last element of result if their combined length is less than charLimit
        String lastElement = result.get(result.size() - 1);
        if (lastElement.length() + 1 + splitted[splitted.length - 1].length() < charLimit) {  //+1 for space

            result.set(result.size() - 1, lastElement + " " + splitted[splitted.length - 1]);
        } else {
            result.add(splitted[splitted.length - 1]);
        }

        //append message chunk number for ex (1/3)
        int resultSize = result.size();
        for(int i = 0; i < resultSize; i++) {
            result.set(i, result.get(i) +"("+ (i+1) + "/" + resultSize + ")" );
        }

        return result;
    }
    public static void main(String[] args) {
        String message;
        int charLmit;
        message = "Hi Sivasrinivas, your Uber is arriving now! And this is a bigger text";
        charLmit = 25;
        ArrayList<String> result = splitText(message, charLmit);
        for(String item : result) {
            System.out.println("Length = " + item.length() + " : " +item  );
        }
        System.out.println(result.toString());
    }
}

- vicky17D November 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static List<string> GetChunks(string message, int messageLimit)
{
   var result = new List<string>();
   int mbX = 0, mbY = -1;
   do
   {
       mbX = mbY + 1;
       mbY = mbX + messageLimit;
       while (mbY >= mbX && mbY < message.Length && message[mbY--] != ' ' );
       if (mbY < mbX) throw new Exception("Cannot chunk."); 
       if (mbY >= message.Length) mbY = message.Length;
       result.Add(message.Substring(mbX, mbY - mbX));
   } while (mbY >= message.Length -1);
   return result;
}

- ankushbindlish November 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void splitString(String message, int charLimit)
{
if( message != null && charLimit > 0)
{
String[] words = message.split(" ");
ArrayList<String> resultList = new ArrayList<String>();
String newWord="";
for(int i = 0; i< words.length; i++)
{
String checkWord = String.format("%s %s", newWord,words[i]);
if( checkWord.length() <= charLimit)
{
newWord = String.format("%s %s", newWord,words[i]);
if(i==words.length-1)
{
resultList.add(newWord);
}
}
else
{
resultList.add(newWord);
newWord = words[i];
}
}
int resultListLength = resultList.size();
for(int i = 0; i < resultListLength; i ++)
{
String finalWord = resultList.get(i);
finalWord = String.format("%s(%s/%s)", finalWord,i+1,resultListLength);
System.out.println(finalWord);
}


}
}

- Rajeev Tomar November 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

"your Uber is arriving(2/3)" is 26 characters. Fail!

- VladFein November 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My C implementation.Basically prints it on screen instead of returning.

#include <stdio.h>
// Space needed to store "(XXX/YYY)"
#define POSTFIX 10 

void spacefinder(char *str,int size,int st,int *end){
	int lastspace;
	int i;
	i=st;
	lastspace=st;
	*end=st;
	
	while(str[i]!='\0'){
		if(str[i]!=' ')
			(*end)++;
		else{
			// Check if total size is as expected.
			if(*end-st <= size - POSTFIX){
				lastspace=*end;
				(*end)++;
			}
			else
				break;
		}
		i++;
	}
	// If no space found or string ends return *end , otherwise return lastspace.
	if(lastspace!=st && str[i]!='\0' )
		*end=lastspace;
		
	return;
}

// Takes string and split size ( assumes size > 10 )
void splitstring(char *str,int size){
	
	int i=0,j;
	int end;
	int len;
	int count = 0;
	char **split;
	char c[POSTFIX];
	char temp[POSTFIX];
	c[0]='(';
	len=strlen(str);
	//Malloc 2d array
	split=(char**)malloc (sizeof(char*));
	
	while(i<len){
		count++;
		//Keep on increasing using realloc
		split=(char**)realloc (split,sizeof(char*)*count);
		split[count-1]=(char*)malloc (sizeof(char)*strlen(str));
		spacefinder(str,size,i,&end);
		//Copy result in split[count-1]
		strncpy(split[count-1],str+i,end);
		split[count-1][end-i]='\0';
		i=end+1;
	}
	
	//Steps to append total number of messages "/XXX)" .
	temp[0]='/';
	itoa(count,temp+1,10);
	strcat(temp,")");
	
	//Steps to prepend count of message "(XX" and printing.
	for(j=0;j<count;j++){
		itoa(j+1,c+1,10);
		strcat(c,temp);
		strcat(split[j],c);
		printf("Split string = %s,len=%d\n",split[j],strlen(split[j]));
	}
	
}

- NewGuy November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My POSTFIX is storing "(XXX/YYY)" instead of "(x/y)". So the final strings might look different.Please change POSTFIX to reflect the correct string.

- NewGuy November 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static List<String> generateSplit(String text, int limit) {
        //asserts here
        int suffixLength = " (x/x)".length();
        List<StringBuilder> answer = new ArrayList<>();
        String[] wordsArray = text.split("\\s+");

        int loop = 1;
        while (true) {
            List<String> words = new ArrayList<>(Arrays.asList(wordsArray));

            while (!words.isEmpty()) {
                //form a line
                StringBuilder sb = new StringBuilder();
                while (!words.isEmpty() && sb.length() + suffixLength + words.get(0).length() + 1 <= limit) {
                    sb.append(words.remove(0)).append(' ');
                }
                answer.add(sb.deleteCharAt(sb.length()-1));
            }
            if (answer.size() >= Math.pow(10, loop)) {
                loop++;
                suffixLength += 2;
            } else {
                break;
            }
        }
        for (int i = 0; i < answer.size(); i++) {
            answer.get(i).append(" (").append(i + 1).append("/").append(answer.size()).append(')');
        }
        List<String> result = new ArrayList<>(answer.size());
        for (StringBuilder sb : answer) {
            result.add(sb.toString());
        }
        return result;
    }

- Igor April 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str1="Hi RAJ, your Uber is on its way!"
words = str1.split()
lens=len(words)
increment=3
start=0
while increment<=lens:
print words[start:increment]
start=increment
increment=increment+2

- RAJ May 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string>
divide_str(const string& in,
           size_t chunk)
{
    vector<string> out;
    if (in.empty()) {
        return out;
    }

    unsigned start = 0;
    while (start < in.length()) {
        unsigned end = min(start + chunk, in.length()) - 1;
        bool skip = false;
        if (end == in.length() - 1) {
            skip = true;
        } else if (end < in.length() && in[end + 1] == ' ') {
            skip = true;
        }
        unsigned next_start = end + 1;
        if (!skip) {
            while (start < end && in[end] != ' ') {
                --end;
            }
            next_start = end + 1;
            while (start < end && in[end] == ' ') {
                --end;
            }
        }
        out.push_back(in.substr(start, end-start+1));
        start = next_start;
    }

    return out;
}

int main()
{
    string str = "Hello World! How are things with you? Not bad!";
    vector<string> out = divide_str(str, 12);
    for (int i = 0; i < out.size(); i++) {
        cout << "(" << i + 1<< "/" << out.size() << ")" << out[i] << endl;
    }
    return 0;
}

- Bankim May 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string>
divide_str(const string& in,
           size_t chunk)
{
    vector<string> out;
    if (in.empty()) {
        return out;
    }

    unsigned start = 0;
    while (start < in.length()) {
        unsigned end = min(start + chunk, in.length()) - 1;
        bool skip = false;
        if (end == in.length() - 1) {
            skip = true;
        } else if (end < in.length() && in[end + 1] == ' ') {
            skip = true;
        }
        unsigned next_start = end + 1;
        if (!skip) {
            while (start < end && in[end] != ' ') {
                --end;
            }
            next_start = end + 1;
            while (start < end && in[end] == ' ') {
                --end;
            }
        }
        out.push_back(in.substr(start, end-start+1));
        start = next_start;
    }

    return out;
}
 
int main()
{
    string str = "Hello World! How are things with you? Not bad!";
    vector<string> out = divide_str(str, 12);
    for (int i = 0; i < out.size(); i++) {
        cout << "(" << i + 1<< "/" << out.size() << ")" << out[i] << endl;
    }
    return 0;
}

- Bankim May 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string>
divide_str(const string& in,
           size_t chunk)
{
    vector<string> out;
    if (in.empty()) {
        return out;
    }

    unsigned start = 0;
    while (start < in.length()) {
        unsigned end = min(start + chunk, in.length()) - 1;
        bool skip = false;
        if (end == in.length() - 1) {
            skip = true;
        } else if (end < in.length() && in[end + 1] == ' ') {
            skip = true;
        }
        unsigned next_start = end + 1;
        if (!skip) {
            while (start < end && in[end] != ' ') {
                --end;
            }
            next_start = end + 1;
            while (start < end && in[end] == ' ') {
                --end;
            }
        }
        out.push_back(in.substr(start, end-start+1));
        start = next_start;
    }

    return out;
}

int main()
{
    string str = "Hello World! How are things with you? Not bad!";
    vector<string> out = divide_str(str, 12);
    for (int i = 0; i < out.size(); i++) {
        cout << "(" << i + 1<< "/" << out.size() << ")" << out[i] << endl;
    }
    return 0;
}

- interviewHacker May 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void print(string input, int limit)
    {
        int start = 0, end = 0;
        while(true)
        {
            if (end >= input.Length)
            {
                Console.WriteLine(input.Substring(start, input.Length - start));
                return;
            }

            if (end - start + 1 > limit)
            {
                if(input[end] != ' ')
                {
                    while (input[--end] != ' ')
                    {
                    }
                }

                Console.WriteLine(input.Substring(start, end - start));
                start = ++end;
            }
            else
            {
                end++;
            }
        }
    }

- Anonymous June 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<String> splitText(String s,int limit)
{
List<String> sentences=new ArrayList<String>();
List<String> result=new ArrayList<String>();
if(s==null || s.isEmpty()) return sentences;
limit=limit-"(x/x)".length();
String[] words=s.split("\\s+");
for(int i=0;i<words.length;i++)
{
StringBuilder sb=new StringBuilder();
sb.append(words[i]);
while(i+1<words.length && sb.length()+words[i+1].length()+1<=limit)
{
sb.append(" "+words[i+1]);
i++;
}
sentences.add(sb.toString());
}
int size=sentences.size();
for(int j=0;j<sentences.size();j++)
{
result.add(sentences.get(j)+"("+(j+1)+"/"+size+")");
}
return result;
}

- Anonymous August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<String> splitText(String s,int limit)
	{
		List<String> sentences=new ArrayList<String>();
		List<String> result=new ArrayList<String>();
		if(s==null || s.isEmpty()) return sentences;
		limit=limit-"(x/x)".length();
		String[] words=s.split("\\s+");
		for(int i=0;i<words.length;i++)
		{
			StringBuilder sb=new StringBuilder();
			sb.append(words[i]);
			while(i+1<words.length && sb.length()+words[i+1].length()+1<=limit)
			{
				sb.append(" "+words[i+1]);
				i++;
			}
			sentences.add(sb.toString());
		}
		int size=sentences.size();
		for(int j=0;j<sentences.size();j++)
		{
			result.add(sentences.get(j)+"("+(j+1)+"/"+size+")");
		}
		return result;
	}

- souleater August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<String> splitText(String s,int limit)
	{
		List<String> sentences=new ArrayList<String>();
		List<String> result=new ArrayList<String>();
		if(s==null || s.isEmpty()) return sentences;
		limit=limit-"(x/x)".length();
		String[] words=s.split("\\s+");
		for(int i=0;i<words.length;i++)
		{
			StringBuilder sb=new StringBuilder();
			sb.append(words[i]);
			while(i+1<words.length && sb.length()+words[i+1].length()+1<=limit)
			{
				sb.append(" "+words[i+1]);
				i++;
			}
			sentences.add(sb.toString());
		}
		int size=sentences.size();
		for(int j=0;j<sentences.size();j++)
		{
			result.add(sentences.get(j)+"("+(j+1)+"/"+size+")");
		}
		return result;

}

- Ravi August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Javascript implementation. Not happy about the two loops though. :/

function splitMsg(msg, limit) {
  var chunks = [];
  var split = msg.split(' ');

  var thisChunk = split[0];
  for (var i=1; i < split.length; i++) {
    var nextLength = thisChunk.length + split[i].length + 1;

    if (nextLength > (limit-5)) {
      chunks.push(thisChunk);
      thisChunk = split[i];
    } else {
      thisChunk += " " + split[i];
    }

    if (i == split.length-1) chunks.push(thisChunk);
  }

  return chunks.map(function(chunk, i) {
    return chunk + "("+(i+1)+"/"+chunks.length+")";
  });
}

- andersoneric777 September 14, 2016 | 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