Skip to content

Linked List Interview Questions and Solutions #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
--wip-- [skip ci]
  • Loading branch information
amejiarosario committed Aug 25, 2020
commit 12f17b5ac00c05217df168ee7d2d5cc30a5594be
41 changes: 41 additions & 0 deletions book/D-interview-questions-solutions.asc
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,44 @@ include::interview-questions/buy-sell-stock.js[tag=solution]
----

The runtime is `O(n)` and a space complexity of `O(1)`.

:leveloffset: +1

=== Solutions for Linked List Questions
(((Interview Questions Solutions, Linked Lists)))

:leveloffset: -1

[#linkedlist-q-merge-lists]
include::content/part02/linked-list.asc[tag=linkedlist-q-merge-lists]

Algorithm:

- TODO
- Next TODO

[source, javascript]
----
include::interview-questions/merge-lists.js[tag=description]
include::interview-questions/merge-lists.js[tag=solution]
----

The runtime is `` and a space complexity of ``.



// [#linkedlist-q-merge-lists]
// include::content/part02/linked-list.asc[tag=linkedlist-q-merge-lists]

// Algorithm:

// - TODO
// - Next TODO

// [source, javascript]
// ----
// include::interview-questions/merge-lists.js[tag=description]
// include::interview-questions/merge-lists.js[tag=solution]
// ----

// The runtime is `` and a space complexity of ``.
4 changes: 2 additions & 2 deletions book/content/part02/array.asc
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ To sum up, the time complexity of an array is:
// tag::array-q-max-subarray[]
===== Max Subarray

Given an array of integers, find the maximum sum of consecutive elements (subarray).
A-1) Given an array of integers, find the maximum sum of consecutive elements (subarray).
// end::array-q-max-subarray[]

[source, javascript]
Expand All @@ -297,7 +297,7 @@ _Solution: <<array-q-max-subarray>>_
// tag::array-q-buy-sell-stock[]
===== Best Time to Buy and Sell an Stock

You are given an array of integers. Each value represents the closing value of the stock on that day. You are only given one chance to buy and then sell. What's the maximun profit you can obtain? (Note: you have to buy first and then sell)
A-2) You are given an array of integers. Each value represents the closing value of the stock on that day. You are only given one chance to buy and then sell. What's the maximun profit you can obtain? (Note: you have to buy first and then sell)
// end::array-q-buy-sell-stock[]

[source, javascript]
Expand Down
18 changes: 16 additions & 2 deletions book/content/part02/linked-list.asc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ Use a doubly linked list when:

For the next two linear data structures <<part02-linear-data-structures#stack>> and <<part02-linear-data-structures#queue>>, we are going to use a doubly linked list to implement them. We could use an array as well, but since inserting/deleting from the start performs better with linked-lists, we are going use that.

==== Linked List Exercises
==== Interview Questions
(((Interview Questions, Arrays)))

1) Merge two sorted lists into one (and keep them sorted)
// tag::linkedlist-q-merge-lists[]
===== Max Subarray

LL-1) Merge two sorted lists into one (and keep them sorted)
// end::linkedlist-q-merge-lists[]

[source, javascript]
----
include::../../interview-questions/merge-lists.js[tag=description]
// write you code here
}
----

_Solution: <<linkedlist-q-merge-lists>>_
36 changes: 36 additions & 0 deletions book/interview-questions/merge-lists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const ListNode = require('../../src/data-structures/linked-lists/node');
// tag::description[]
/**
* Given two sorted linked lists merge them while keeping the asc order.
* @examples
* mergeTwoLists([2,4,6], [1,3]); // => [1,2,3,4,6]
* mergeTwoLists([2,4,6], []); // => [2,4,6]
* mergeTwoLists([], [1,3]); // => [1,3]
* @param {number[]} prices - Array with daily stock prices
*/
function mergeTwoLists(l1, l2) {
// end::description[]
// tag::solution[]
const l0 = new ListNode();
let i0 = l0;
let i1 = l1;
let i2 = l2;

while (i1 || i2) {
if (!i1 || (i2 && i1.value > i2.value)) {
i0.next = i2;
i2 = i2.next;
} else {
i0.next = i1;
i1 = i1.next;
}

i0 = i0.next;
}

return l0.next;
}
// end::solution[]


module.exports = { mergeTwoLists };
22 changes: 22 additions & 0 deletions book/interview-questions/merge-lists.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { mergeTwoLists } = require('./merge-lists');
const LinkedList = require('../../src/data-structures/linked-lists/linked-list');
// const ListNode = require('../../src/data-structures/linked-lists/node');

describe('Linked List: Merge Lists', () => {
function asString(root) {
const values = [];
for (let curr = root; curr; curr = curr.next) {
values.push(curr.value);
}
return values.join(' -> ');
}

it('should merge in asc order', () => {
const l1 = new LinkedList([2, 3, 4]).first;
const l2 = new LinkedList([1, 2]).first;
console.log({l1: asString(l1), l2: asString(l2)});
const actual = mergeTwoLists((l1, l2));
const expected = '1 -> 2 -> 2 -> 3 -> 4';
expect(asString(actual)).toEqual(expected);
});
});