✒️
Notes
  • Lirenxn's daily notes
  • Featured
    • Concurrent on fiber and requestIdleCallback
    • Deep dive eslint
      • eslint with class property
      • eslint with fix
      • eslint with jsx
    • A opinioned intro to React Hooks
      • React Hooks 1 - An Overview
      • React Hooks 2 - A mindset change
      • React Hooks 3 - Test Hooks
    • Handy tricks
    • About Micro FrontEnd
    • Same-site cookie
    • Thoughts
      • Application Security for frontend OWASP
      • Javascript this
      • React validation
  • 2020 Daily Notes
    • Notes-04-05-eslint-and-babel
    • Notes-04-03-Hard-interview-question
    • Notes-03-31-ReactContext-JS-inherit
    • Notes-03-28-digital-signing-webpack-dynamic-import-JsonRest-Promise-pattern-performance.measure()
    • Wk-notes-03-16-i18n-solution
    • Notes-03-15-Concurrent-with-requestIdleCallback
    • Notes-03-15-Micro-frontend
    • Wk-notes-03-09-micro-frontend-bootstrapper
    • Wk-notes-02-27-API-design-(g)RPC-REST-etc
    • Wk-notes-02-26-React-form-validation-Boolean-algebra-Rule-for-this-App-security-npm-devtool-tricks
    • Wk-notes-02-18-i18n-gRPC
    • Wk-notes-02-11-Gradual-rollout-webpack-vscode-auto-import
    • Note-02-09-spring-webpack
    • Wk-notes-02-06-more-webpack
    • Wk-notes-02-05-Learn-spring-webpack
    • Wk-notes-02-04-props-drilling-virtual-list
    • Wk-notes-02-03-same-site-cookie
    • Wk-notes-01-31-g-recaptcha-npmshrinkwrap
    • Wk-notes-01-30-React-ref
    • Wk-notes-01-23-remote-android
    • Wk-notes-01-23-test-hook
    • Wk-notes-01-22-about-Hooks
    • Wk-notes-01-17-bundling-browser-module-treeshake-2
    • Wk-notes-01-16-Bundling-Treeshaking-1
    • Wk-notes-01-13-npm-script-css-scroll
    • Wk-notes-01-13-touchscreen-hover-and-apis
    • Wk-notes-01-13-emotion-x
    • Wk-notes-01-10-codemod
    • Wk-notes-01-08-live-region-react-test-lib
    • Wk-notes-01-06-eslint-finalise
  • 2019 Daily Notes
    • Wk-notes-12-11-eslint-dive-vscode-debug
    • Wk-notes-12-10-eslint-dive
    • Wk-notes-12-6-splunk
    • Wk-notes-12-3-react-function-memo
    • Wk-notes-12-2-agile-test-micro-frontend
    • Wk-notes-11-29-npm-fix-aem-datalayer-test
    • Wk-notes-11-28-adobe-dataLayer
    • Wk-notes-11-27-a11y
    • Wk-notes-11-25-upgrade-preact
    • Wk-notes-11-22-a11y-n-voice-over
    • Wk-notes-11-21-Add-Typescript
    • Wk-notes-11-20-JSDoc
    • Wk-notes-11-19-A11y-Polyfill
    • Wk-notes-11-18-jest-mock
    • Wk-notes-11-15-React-Portal
    • Wk-notes-11-14-iOS-simulator-git-hooks-All-hands-testing
    • Wk-notes-11-13-i18n
    • Wk-notes-11-12-safari-remote-debug
    • Wk-notes-11-11-migrating-typescript-git-remote-label-Emotion-controlled-component
    • Wk-notes-11-08-living-pricing-arch
    • Wk-notes-11-07-vitual-box-n-onblur-for-div
    • Wk-notes-11-06-bug-race-virtual-dom-diff
    • Wk-notes-11-05-babel-loader-eslint
Powered by GitBook
On this page

Was this helpful?

  1. 2020 Daily Notes

Notes-04-03-Hard-interview-question

To sharpen your mind

PreviousNotes-04-05-eslint-and-babelNextNotes-03-31-ReactContext-JS-inherit

Last updated 5 years ago

Was this helpful?

3 Hard interview white board

Ref:

Auto completion

Given a list of strings, with typed in partial query, output potential words.

In ['dog', 'dark', 'cat', 'door', 'dodge', 'do'], with input do, ['dog', 'door', 'dodge'] should be returned (NOT 'do'), number of letter k, list size n

Bruto iteration

Iterate for each letter input, O(kn)

Use trie

  • For search: O(k) + O(n) = O(n)

  • For build: O(ln), l being the average length of words

class Node{
	constructor(value, children){
		this.value = value;
		this.children = children || {};
	}

	addChild(node){
		this.children[node.value] = node;
	}
}

class Solve{
	constructor(data){
		this.trie = this.build(data);
	}

	rebuild(data){
		this.trie = this.build(data);
	}

    // Snippet build trie
	build(data){
		const root = new Node('');
		for(let word of data){
			let nodeItr = root;
			for(let letter of word){
				if(!nodeItr.children[letter]){
					nodeItr.children[letter] = new Node(letter);
				}
				nodeItr = nodeItr.children[letter]	
			}
			nodeItr.children['EOF'] = new Node('EOF');
		}
		return root;
	}

	getMatch(prefix, selfIncluded){
		let nodeItr = this.trie;
		let res = [];
		// Snippet: Traverse trie
		for(let letter of prefix){
			if(!nodeItr.children[letter]){
				return res;
			}
			nodeItr = nodeItr.children[letter];
		}
		res = this.getChildren(nodeItr).map(child => prefix + child.substr(1))
		if(!selfIncluded){
			res = res.filter(s => s !== prefix);
		}
		return res;
	}
	
	// Snippet recursion
	getChildren(node){
	    // End Condition
		if(node.value === 'EOF'){
			return [''];
		}
		// Do recurrsion
		const children = Object.keys(node.children).reduce((acc, key) => {
			return acc.concat(this.getChildren(node.children[key]));
		}, []);
		return children.map(child => node.value + child);
	}
}

// Tests
pb = new Solve(["do","dod","dog",'dark',"apple","dodge","door","doddle"]);
pb.getMatch('do');

pb.getMatch('dod');

pb.getMatch('dod', true);

Gotchas

  • in Node REPL, use process.pwd() to check pwd

  • in Node REPL, use require() works the same as in a file, if it does not work, check path

  • in Node REPL, use require.cache = {} to reset the cache otherwise following require will use the cache

  • in Node, use module.exports to properly export

K-th largest Number

Given list of numbers [5,7,2,3,4,1,6], find 3-th largest number

Bruto

  • Traverse k times of array, each time return the largest one, O(kn)

  • Sort first then get index, O(nlog)

(Max) Heap

  • For build: O(n), average complexity from Calculus

  • For search: pop k times, each pop traverse the tree depth logn: O(klogn)

Partition

  1. Pick the last number as delimiter

  2. Partition the array as left smaller right larger

  3. Partition the right(larger) partition until the size of large partition (sum of right partitions) equals k

Check if string concatable from list of words

Given ['cats', 'cat', 'dog'] and "catsdog", get true as catsdog = cats + dog, case also possible as ahardlongword = a + hard + long + word

Bruto

c atsdog
    a tsdog
        t sdog
        ts dog
        tsd og
        tsdo g
            ...
    at sdog
    ats dog
    atsd og
    atsdo g
ca tsdog ...
cat sdog ...
cats dog ...
catsd og ...
catsdo g ...

O(n2^m)

https://www.youtube.com/watch?v=QGVCnjXmrNg