HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptMinor

Based on a file containing people's names, output some statistics and generate some modified names

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
containingfileoutputnamesmodifiedgeneratestatisticspeoplebasedsome

Problem

Noobie here. I applied for a job as a Jr. engineer and they gave me this task. After they read through my solution they rejected my application because the code was hard to follow. I'd like to get an idea of what they meant by that and how I can improve the approach I took. The full assignment is below.


Write a program that processes the included example file that contains
people's names, outputs some statistics and generates updated output.


Input:


An arbitrary file with the same format as the attached example file.
Your program will be tested on much (e.g. 1000x) larger files.


Example lines from file:

Graham, Mckenna -- ut
    Voluptatem ipsam et at.
Marvin, Garfield -- non
    Facere et necessitatibus animi.
McLaughlin, Mariah -- consequatur
    Eveniet temporibus ducimus amet eaque.
Lang, Agustina -- pariatur
    Unde voluptas sit fugit.
Bradtke, Nikko -- et
    Maiores ab officia sed.
Adams, Luis -- error
    Repellendus alias officia amet et perspiciatis.
Lehner, Matilde -- nesciunt
    Incidunt et ut necessitatibus porro.
Ortiz, Anita -- fuga
    Tempore eos et hic.
Koch, Berry -- vel
    Laborum perferendis inventore eveniet.
Cartwright, Nicolas -- et
    Optio aliquid earum exercitationem vitae fugit.
Fisher, Elmo -- non
    Ipsum provident nobis explicabo voluptas ipsa aperiam.



Output:



-
The unique count of full, last, and first names (i.e. duplicates are counted only once)

-
The ten most common last names (the names and number of occurrences)

-
The ten most common first names (the names and number of occurrences)

-
A list of 25 completely unique names (using names within the file)

-
A list of 25 modified names (must be new names made up of the 25 names from list number 4. Must be made by switching first and last names)


```
var fs = require('fs');
var async = require('async');
var readline = require('readline');
var prompt = require('prompt');
var fullNameHash = {};
var firstNameHash = {};

Solution

The essential issue here is that there's just too much code. The complexity of the problem doesn't merit over 200 lines of code.

You're transforming some regularly formatted text into a list of names.
Then answering some questions about those lists.

Critiquing your code line by line just won't be that helpful in this case, because, although there are some things to improve, what you really want to do is just take a simpler approach at a high level. Here's one (of many) ways you might do that:

// a few object utilities
///////////////////////////
const length = o => Object.keys(o).length;
const byCount = (a, b) => b[1] - a[1];
const top10 = o => Object.entries(o).sort(byCount).map(x => x[0]).slice(0,10);

function addItem(countObj, key) {
  const newKey = (countObj[key] === undefined);
  if (newKey) countObj[key] = 0;
  countObj[key]++;
}    
// get the name counts
///////////////////////////
const lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('names.txt')
});

const [firstNames, lastNames, fullNames] = [{}, {}, {}];

lineReader.on('line', function (line) {
  const matches = line.match(/(.*), (.*) -- .*/);
  if (!matches) return;
  const [_, last, first] = matches;

  addItem(firstNames, first);
  addItem(lastNames, last);
  addItem(fullNames, first + ' ' + last);
});   
// print results
///////////////////////////   
lineReader.on('close', function (line) {
  console.log('Unique first: ', length(firstNames));
  console.log('Unique last: ' , length(lastNames));
  console.log('Unique full: ' , length(fullNames));
  console.log('Top 10 first: ', top10(firstNames));
  console.log('Top 10 last: ' , top10(lastNames));
});


Note that the data structure I'm using here is a plain object, whose keys are the names and whose values are the number of times that name appears.

I'll leave the final questions as an exercise, because it wasn't entirely clear to me what those questions were asking.

Code Snippets

// a few object utilities
///////////////////////////
const length = o => Object.keys(o).length;
const byCount = (a, b) => b[1] - a[1];
const top10 = o => Object.entries(o).sort(byCount).map(x => x[0]).slice(0,10);

function addItem(countObj, key) {
  const newKey = (countObj[key] === undefined);
  if (newKey) countObj[key] = 0;
  countObj[key]++;
}    
// get the name counts
///////////////////////////
const lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('names.txt')
});

const [firstNames, lastNames, fullNames] = [{}, {}, {}];

lineReader.on('line', function (line) {
  const matches = line.match(/(.*), (.*) -- .*/);
  if (!matches) return;
  const [_, last, first] = matches;

  addItem(firstNames, first);
  addItem(lastNames, last);
  addItem(fullNames, first + ' ' + last);
});   
// print results
///////////////////////////   
lineReader.on('close', function (line) {
  console.log('Unique first: ', length(firstNames));
  console.log('Unique last: ' , length(lastNames));
  console.log('Unique full: ' , length(fullNames));
  console.log('Top 10 first: ', top10(firstNames));
  console.log('Top 10 last: ' , top10(lastNames));
});

Context

StackExchange Code Review Q#144253, answer score: 2

Revisions (0)

No revisions yet.