Goldman Sachs Interview Question

hackerrank : positional element and string reduce