Uber | Phone | Break Messages

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-03-13Added By Anonymous
Source: Link

Company: Uber

Interview Stage: Phone Screen


Original Post Content:


Problem

Write a function that takes a big message and splits it into minimum number of smaller messages adhering to given limit. While splitting the function should not split a word into two messages. In the output messages please add tag ( x/y ). The total length (including the tag) should not exceed 160.


Constraints


Each chunk:


- up to 160 characters long

- no word should be split in the middle

- each chunk has to have its order suffixed in the form of ' (k/n)', e.g. "this is the first chunk (1/2)", "this is the second chunk (2/2)"

- if the text provided to the function is less than 160 characters, no ordering should be suffixed


Input:


The South Lake Union Streetcar is a streetcar route in Seattle, Washington, United States. Traveling 1.3 miles (2.1 km), it connects downtown to the South Lake Union neighborhood on Westlake Avenue, Terry Avenue, and Valley Street. It was the first modern Seattle Streetcar line, beginning service on December 12, 2007, two years after a separate heritage streetcar ceased operations. It was conceived as part of the redevelopment of South Lake Union into a technology hub, with lobbying and financial support from Paul Allen.


Output Not fully correct just to make it easy for you to imagine it :)


Array of messages


The South Lake Union Streetcar is a streetcar route in Seattle, Washington, United States. Traveling 1.3 miles (2.1 km), it connects downtown to the South (1/4)

Lake Union neighbourhood on Westlake Avenue, Terry Avenue, and Valley Street. It was the first modern Seattle Streetcar line, beginning service on December (2/4)

12, 2007, two years after a separate heritage streetcar ceased operations. It was conceived as part of the redevelopment of South Lake Union into (3/4)

a technology hub, with lobbying and financial support from Paul Allen. (4/4)


Solution Implemented

1def messageList(message): 2if not message: 3return "Message is Empty!" 4if len(message) <= 160: 5return message 6 7msgs_map = {} 8message_words = message.split() 9current_message = '' 10current_message_number = 1 11for word in message_words: 12if len(current_message + ' ' + word) > 154: 13msgs_map[current_message_number] = current_message 14current_message = word 15current_message_number += 1 16else: 17current_message += word + ' ' 18 19n = len(msgs_map) 20messages = [] 21for k in msgs_map: 22messages.append(msgs_map[k] + ' (' + str(k) + '/' + str(n) + ')') 23 24return messages 25 26 27if __name__ == '__main__': 28T = int(input()) 29for _ in range(T): 30message = input() 31print(messageList(message))


Analysis I did, I am not sure if it is correct?

1n ==> # of words in provided message 2m ==> # of the broken messages 3O (n + m)


Suggested solution without having time to implement it

1by first find the n which is the number of messages we can have and do it in this way: 2 3len(bigMessage)/154 => number of broken messages 4 5Then go over the words build the list in one path now we will be improved the space 6to only O(w + n) : w => # of words, n # of messages 7Time complexity will be improved to only we do one path to build the messages list, 8don't forget the split function that it takes O(n) 9so total O(n + n) ==> O(2n) ==> O(n)


If anyone has any idea about what could be the best solution I will be happy to know it :).

Comments