0
votes
0answers
13 views

Best algorithm to find a subarray whose sum is closer or equal to given number. [on hold]

Suppose we have sub array of size n and we have to find subarray of whose sum is equal to k or closest to k. Solution: What would be best algo for this problem.
1
vote
0answers
32 views

Deepest left leaf node in a binary tree

Given a Binary Tree, find the deepest leaf node that is left child of its parent. This question is attributed to GeeksForGeeks. Looking for code-review, optimizations and best practices. ...
6
votes
2answers
75 views

Flatten a multilevel linked list

Given such a structure the output should be ...
3
votes
1answer
42 views

Reverse values of alternate nodes of the tree

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree. Given tree: ...
0
votes
0answers
23 views

Return the next right node

Given a binary tree, return the next right node. This question is attributed to GeeksForGeeks. For example, consider the following Binary Tree. Output for 2 is 6, output for 4 is 5. Output for 10, 6 ...
4
votes
1answer
30 views

Synchronized LinkedHashed map

I've written the following code ages ago (10+ years) which is part of a simple chat server. I'm going to refactor it a bit in java and then for fun I'm going to convert it to Scala and use Akka actors ...
1
vote
1answer
66 views

Get the levels of a binary tree

Given a binary tree, return a list of each level. I'm looking for a general review and a mention on best-practices, optimization, and verification of my complexities. Time complexity: \$O(n)\$ ...
0
votes
0answers
30 views

Nested maps vs. combined keys [migrated]

in the project I am currently working on we had three different types of prices depending on the age of the user (adult, child, etc...). So we had on the DB a table looking like this: ...
1
vote
0answers
23 views

Following-up “given a two dimensional linked list, create a flattened sorted list”

A node has a next and down pointer. Merge the linked-list such that end result is sorted. Here we have a top level linked-list, followed by a down-list. If { 10 : {20, 30} , 35 { 40, 50 } ...
2
votes
2answers
44 views

Merge N sorted list

Given n sorted lists, merge them. This is a fairly common question, attributed to plenty of interview question websites. I'm looking for code-review, optimizations and best practices. I'm also ...
3
votes
3answers
174 views

Find all nodes in tree, without a sibling

Find all nodes of trees, without siblings. This question is attributed to GeeksForGeeks. I'm looking for code reviews, optimizations and best practices. ...
0
votes
3answers
117 views

Given a two dimensional linked list, create a flattened sorted list

A node has a next and down pointer. Merge the linked-list such that end result is sorted. Here we have a top level linked-list, followed by a down-list. If { 10 : {20, 30} , 35 { 40, 50 } } is the ...
1
vote
0answers
34 views

Find K distant nodes

Find all nodes, which are at a distance of k from the input node. Input: target = pointer to node with data 8. ...
5
votes
2answers
101 views

Queue over resizable array implementation

I study data structures on coursera's course, and there is an extra exercise to create a Queue data structure. I created it: ...
4
votes
3answers
134 views

Repeatedly skip M nodes then delete N nodes then skip M nodes and so on

Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same until end of the linked list. Input: ...
-2
votes
1answer
89 views

Given a BST, transform it into greater sum tree

Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node. Diagram is here. Looking for code review, optimizations and best practices. ...
1
vote
1answer
57 views

Sort a linkedlist with 3 elements 0, 1 and 2

Given a linked list of 0s, 1s and 2s, sort it. Looking for code review, optimizations and best practices ...
6
votes
2answers
132 views

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree. Given tree: ...
0
votes
0answers
33 views

Detect if a tree is complete binary tree

Detect if tree is complete binary tree. This question is improvement over previously asked here. Looking for code-review, optimizations and best practices. Verifying both space and time complexity to ...
2
votes
4answers
101 views

Maximum of all subarrays of size k

Given an array and an integer k, find the maximum for each and every contiguous subarray of size k. Examples: Input: ...
2
votes
2answers
49 views

Decide the start of the circular gas station path

Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data. The amount of petrol that petrol pump will give. Distance from that petrol pump to the ...
5
votes
2answers
141 views

Find max-number div by three to be formed by array elements

Given an array of non-negative integers, find the largest multiple of 3 that can be formed from array elements. For example, if the input array is {8, 1, 9}, the output should be "9 8 1", and ...
6
votes
4answers
144 views

Stack with 'getMinimum' operation

Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the ...
4
votes
1answer
47 views

Stack with “getMiddle” and “deleteMiddle” operation

Looking for code review, optimizations and best practices. ...
4
votes
2answers
92 views

Implement queue using linkedlist

Implement queue using linkedlist. Looking for code review, optimizations and best practices. ...
4
votes
1answer
69 views

Detect a complete binary tree

Detect if a tree is complete binary tree or not. Looking for code review, optimizations and best practices. ...
4
votes
1answer
37 views

Implement stack using a linked list

I am trying to implement Stack using a linked list. Looking for code-review, best practices and optimizations. ...
4
votes
1answer
46 views

Counting inversions

Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion ...
3
votes
2answers
143 views

How is my Java code for stack implementation?

The following example shows how to implement stack by creating user defined push() method for entering elements and pop() method ...
4
votes
1answer
322 views

Queued Employees

The following example shows how to implement a queue in an employee structure. I am looking for a review of the implementation: ...
2
votes
1answer
49 views

M coloring problem

Given an undirected graph and a number m, determine if the graph can be colored with at most m colors such that no two adjacent vertices of the graph are colored with same color. Here coloring of a ...
9
votes
3answers
189 views

Calculating infix expression with no parenthesis

This is my Java implementation (\$O(n)\$) about how to calculate infix expression with no parenthesis. For example, when we input "3 + 4 * 4 / 8", we will get a double type answer is 5.0. Here to ...
0
votes
2answers
96 views

Stack implementation using a queue

I've implemented a stack using a queue. Time complexity: \$O(n)\$ - push(), where n is the number of elements in the queue \$O(1)\$ - ...
3
votes
0answers
55 views

Morris inorder traversal

Morris Inorder traversal - an algorithm for in-order traversal, of a tree, without recursion of extra memory. Looking for code review, optimizations and best practices. ...
7
votes
2answers
138 views

Java implementation of spell-checking algorithm

This little program was written for an assignment in a data structures and algorithms class. This is my first post here, and I understand that I shouldn't post the entire assignment, so in lieu of ...
3
votes
2answers
145 views

BinarySearch Tree implementation & traversals

I am practicing various tree algorithms and that require code review for it's efficiency, clarity and also if it can be made better etc. Is there a better way to call ...
2
votes
4answers
2k views

Depth First Search & Breadth First Search implementation

I've implemented DFS and BFS implementations. I want to check if the code is readable, contains any issues, and can be improved. GraphImplementation ...
1
vote
1answer
58 views

Appropriate insertion sorting algorithm

I am learning data structures, and have written an algorithm for insertion sort after learning from some sources. But I am confused which implementation is more correct or appropriate. Code #1 This ...
6
votes
2answers
1k views

Count frequency of words in a given file

Write a function that takes two parameters: a String representing a text document an integer providing the number of items to return Implement ...
5
votes
2answers
938 views

Comparing list elements in an effective way

I have a list of opened nodes, with each step about 3000 nodes are opened and I want to put to the opened list only those nodes that are not already there. For now I'm just comparing each new one to ...
10
votes
1answer
105 views

Binary Tree/ Amorphous Binary Tree Creator

I suppose this is a two-part question. The first part is just a simple implementation of a Binary Tree (BTree), with pre-order, post-order, and in-order searches ...
6
votes
3answers
138 views

How to extract elements from multiple sorted list efficiently?

Problem Statement:- I was ask this interview question recently.. I was able to come up with the below code only which runs in O(k log n)- Given k <= n sorted arrays each of size n, there exists ...
2
votes
1answer
326 views

Printing the values on each level of a Binary Tree

The below code is for printing level-by-level in a binary tree: ...
10
votes
2answers
173 views

Thread-safe prime number source

I am working on some multi-threaded code that requires a prime-number source. The following code keeps an array of primes in memory, extending it as needed. It works in the 'int' domain, being ...
9
votes
3answers
880 views

Checking brackets nesting in a string

I took a training challenge on Codility that checks for the proper nesting of brackets in a string. The brackets to be checked are {,},(,),[,]. I have written the ...
0
votes
1answer
134 views

Find the mean of the medians of two equal sized sorted arrays [closed]

A corrected solution is found here Looking for code review, clever optimizations, adherence to best practices. etc. As an example: a1[1, 3, 5, 7] a2[2, 4, 6, 8] The median for each array is the ...
3
votes
1answer
138 views

Inorder traversal of a tree without recursion or stack

Please review the code for code cleanup, smart optimizations and best practices. Also verify my complexity: O(n2), where n is the number of nodes ...
3
votes
2answers
130 views

Constructing a binary tree in java

I am constructing a binary tree. Let me know if this is a right way to do it. If not please tell me how to?? I could not find a proper link where constructing a general binary tree has been coded. ...
2
votes
1answer
49 views

Join of two databases

This is the most basic code to join two tables in a database. For tables it uses a simple list. Solved this using brute force, sorting and using hashtable. ...
2
votes
1answer
2k views

Design LRU cache interview questions

Code reviewers, I request you review the code and suggest all tips for improvement. ...