Goldman Sachs interview question

hackerrank : positional element and string reduce