Makeinfo Blog

Makeinfo Blog

Codesignal Challenge - fileNaming

Codesignal Challenge - fileNaming

Dorothi Viki's photo
Dorothi Viki
·May 11, 2022·

1 min read

Note: Please note that experimental code ahead. Only tested in few test cases.


You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an addition to its name in a form of (k), where k is the smallest positive integer such that the obtained name is not used yet.

Return an array of names that will be given to the files.


For names = ["doc", "doc", "image", "doc(1)", "doc"], the output should be fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"].

Source Code

function fileNaming(names) {
  let newArr = [];
  let objVals = {};
    let genName = (name, size) => name + '(' + size + ')';
  for (const name of names) {
    let size = objVals[name] || 0;
    objVals[name] = size + 1;

    if (!size) {

    let newName = genName(name, size);
    while(newArr.includes(newName)) {
      size = size + 1;
      newName = genName(name, size);

    objVals[newName] = size;

  return newArr;

// const names = ["doc", "doc","image", "doc(1)", "doc","doc(1)"];
 const names = [ 'a(1)', 'a(6)',  'a',  'a',  'a',  'a',  'a',  'a','a','a','a','a',];

Test Cases

Only have two test cases and got it working for them.



Share this