employer cover photo
employer logo
employer logo

AQR Capital Management

Engaged Employer

AQR Capital Management Interview Question

You are given an alphanumeric string. Complete the function sortSegments that will segment the string into substrings of consecutive letters or numbers and then sort the substrings. For example, the string "AZQF013452BAB" will result in "AFQZ012345ABB". The input letters will be uppercase and numbers will be between 0 and 9 inclusive.

Interview Answers

Anonymous

Oct 24, 2017

import re def segment_sort(string): segments = re.split('([0-9]+)', string) # Splitting the String into Segments sorted_segments = [''.join(sorted(segment)) for segment in segments] return ''.join(sorted_segments) def main(): string = "AZQF013452BAB" print(segment_sort(string)) if __name__ == '__main__': main()

Anonymous

Nov 10, 2017

A way to do this without regex is to simply try to convert each string character to an int. In python, doing int("A") will result in a ValueError, so you can just use a try-except ValueError clause, and whenever you switch from a region in which you can or cannot convert to int, you know you are at a splitting point, so just save those indices.

Anonymous

May 5, 2018

not sure for python, but for java, all the char have a integer number associated. You can convert char to int, so if the char is greater than '0' and less than '9', this can be an indicator of integer. Use this check you can find all the integer or alpha sub arrays. For each sub array, either add another sort method to sort the char array, or you can use the char comparator default compare method to sort the array. At last, concatenate all the subarrays to form the output.