I have received a takehome test from a company I’m interviewing with and it’s on the platform Codesignal. Luckily, Codesignal provides me with a practice test I can use to test out its platform! It times me and gives me a challenge:
import IntegerContainerInterface from './integerContainerInterface' ;
export default class IntegerContainer extends IntegerContainerInterface {
nums : number []
constructor () {
super ();
this .nums = []
// TODO: implement
}
add ( value : number ) : number {
this .nums. push (value)
return this .nums. length
}
delete ( value : number ) : boolean {
let exists = false
/* failing here
expect(container.add(555)).to.deep.equal(1);
expect(container.delete(555)).to.be.true;
expect(container.delete(555)).to.be.false;
expect(container.add(555)).to.deep.equal(1);
expect(container.delete(555)).to.be.true;
expect(container.delete(555)).to.be.false;
*/
this .nums = this .nums. map ( val => {
if (val === value && exists === false ){
exists = true
return null
}
return val
})
this .nums = this .nums. filter (Boolean)
return exists
}
// TODO: implement interface methods here
}
Takeaways from stage one:
looking at test cases really helped
forEach, map, sort, know what it returns and how it may mutate instead
Stage 2
import IntegerContainerInterface from './integerContainerInterface' ;
export default class IntegerContainer extends IntegerContainerInterface {
nums : number []
constructor () {
super ();
this .nums = []
// TODO: implement
}
add ( value : number ) : number {
this .nums. push (value)
return this .nums. length
}
delete ( value : number ) : boolean {
let exists = false
/* failing here
expect(container.add(555)).to.deep.equal(1);
expect(container.delete(555)).to.be.true;
expect(container.delete(555)).to.be.false;
expect(container.add(555)).to.deep.equal(1);
expect(container.delete(555)).to.be.true;
expect(container.delete(555)).to.be.false;
*/
this .nums = this .nums. map ( val => {
if (val === value && exists === false ){
exists = true
return null
}
return val
})
this .nums = this .nums. filter (Boolean)
return exists
}
// [10, 20]
getMedian () : number | null {
this .nums = this .nums. sort (( a , b ) => a - b)
console. log ({nums: this .nums, length: this .nums. length })
if ( this .nums. length === 0 ) return null
if ( this .nums. length === 1 ) return this .nums[ 0 ]
if ( this .nums. length === 2 ) return this .nums[ 0 ]
// [1, 2, 3, 4] length 4
// 4/2 = 2,
if ( this .nums. length % 2 === 0 ){
return this .nums[( this .nums. length / 2 ) - 1 ]
}
// [1, 2, 3], length is 3, middle index is 1
// So length - 1 / 2,
return this .nums[( this .nums. length - 1 ) / 2 ]
// Return middle number in the .nums array
// If sequence is even, return the leftmost
// If it's empty, return null
}
// TODO: implement interface methods here
}
Man, the mistakes I made were really silly.
For even numbers, I got the correct index, but just straight up returned the index rather than the value AT that index.