Coding Challenge Guidelines
- You can choose any programming language (we’d suggest choosing your strongest)
- You can use the internet, but please don’t look up or copy an existing solution—we want to see how you work
- This isn’t a race, but please track your time so you can tell us how long it took you
- Please don’t spend any longer than 90 minutes total
Evaluation Criteria
Write a library for generating random PIN codes. You probably know what a PIN code is; it’s a short sequence of numbers, often used as a passcode for bank cards.
- The library should export a function that returns a batch of 1,000 PIN codes in random order
- Each PIN code in the batch should be unique
- Each PIN should be:
- 4 digits long
- Two consecutive digits should not be the same (e.g. 1156 is invalid)
- Three consecutive digits should not be incremental (e.g. 1236 is invalid)
- The library should have automated tests.
Technologies
Setup
- Install dependencies
npm install
#or
yarn install
Run tests
npm run test
#or
yarn test
Install the library
npm install pin-generator-1-cazaph
#or
yarn add pin-generator-1-cazaph
https://www.npmjs.com/package/pin-generator-1-cazaph
Usage
Generate a batch of 1,000 PIN codes in random order
import {generatePins, isValidPin} from "pin-generator-1-cazaph";
const pins = generatePins();
console.log(pins);
// ['pin1', 'pin2', 'pin3', ...]
console.log(isValidPin("7801")); // true
console.log(isValidPin("1236")); // false
console.log(isValidPin("1156")); // false
console.log(isValidPin("0023")); // false
console.log(isValidPin("1199")); // false
API
generatePins(count?: number): string[]
This function allows you to generate random pins according to the count. The count is optional and defaults to 1,000.
isValidPin(pin: string, pins?:[]): boolean
This function validates if the pin is valid. To be valid the pin should:
- Have 4 digits long,
- Not contain consecutive digits
- Not contain digits that are incrementally increasing.
- Not be included in the pins array.
The pins parameter is optional and defaults to an empty array.
hasSameConsecutiveDigits(numbers: number[]): boolean
This function checks if the numbers has consecutive digits.
isIncremental(numbers: number[]): boolean
This function checks if the numbers contains 3 more numbers incrementally increasing. e.g. 1239
shuffle(arr: any[]): any[]
This function takes a list of numbers and returns a shuffled version of the same list.
License
The MIT License (MIT). Please see License File for more information.