4
votes
3answers
94 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
43 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. ...
5
votes
2answers
66 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: ...
1
vote
1answer
43 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 ...
2
votes
4answers
84 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: ...
0
votes
0answers
23 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
2answers
45 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 ...
6
votes
4answers
139 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 ...
3
votes
1answer
1k views

Print path (leaf to root) with max sum in a binary tree

I want you to pick my code apart and give me some feedback on how I could make it better or more simple. ...
5
votes
2answers
134 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 ...
4
votes
1answer
46 views

Stack with “getMiddle” and “deleteMiddle” operation

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

Implement queue using linkedlist

Implement queue using linkedlist. Looking for code review, optimizations and best practices. ...
4
votes
1answer
57 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
34 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
44 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
142 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
319 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: ...
5
votes
2answers
622 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 ...
2
votes
1answer
48 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
174 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
87 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)\$ - ...
4
votes
2answers
357 views

Implement data structure overflow queue

Implement data structure overflow queue. Overflow queue is a normal queue with one extra property. It never throw Stack overflow exception. So whenever queue becomes full, it replaces the oldest ...
3
votes
0answers
45 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
122 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
133 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
1k 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
57 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
694 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 ...
4
votes
1answer
1k views

Partitioning array elements into two subsets

Problem: Given a randomly ordered array of n-elements, partition the elements into two subsets such that elements <= x are in one subset and elements x are in the other subset. ...
5
votes
2answers
2k views

Implementation of Dijkstra's algorithm

Please pick my code apart and give me some feedback on how I could make it better or more simple. ...
2
votes
2answers
7k views

Insert sort on a linked list

I want to do insert sort in a linked list without using dummy nodes. This is my code. How can this be improved? Any input is appreciated. ...
9
votes
3answers
742 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 ...
10
votes
1answer
101 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 ...
18
votes
5answers
1k views
6
votes
3answers
131 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 ...
6
votes
1answer
432 views

MaxHeap implementation

I'd like this to be reviewed: ...
10
votes
2answers
157 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 ...
2
votes
1answer
275 views

Printing the values on each level of a Binary Tree

The below code is for printing level-by-level in a binary tree: ...
5
votes
1answer
112 views

Duplicate detection and reporting in a BST

For the purpose of this problem, the BST is defined to allow duplicates, and the duplicate values will always follow to the right-side of the parent nodes with the same value. For example, with the ...
3
votes
2answers
173 views

Least common ancestor in binary tree

This code finds a common ancestor. If one of the input does not exist in the tree then throws an exception. This does not use extra storage. It does not even traverse more than what it should be. It ...
4
votes
1answer
5k views

Create a tree from a list of nodes with parent pointers only

I want you to pick my code apart and give me some feedback on how I could make it better or more simple. ...
-1
votes
1answer
237 views

Join levels of binary tree

Please ignore feedback to include generics and function renaming while giving me feedback. Names of functions have been deliberately kept the way they are for time-being. Also please let me know if ...
2
votes
2answers
290 views

Generic binary search tree implementation

I use this interface for my BST node class: ...
2
votes
1answer
503 views

Least common ancestor for binary search tree

I want you to pick my code apart and give me some feedback on how I could make it better or more simple. This code finds a common ancestor for binary search tree. This code accounts duplicates as ...
0
votes
1answer
131 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
125 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 ...
2
votes
2answers
564 views

Creating an Array of Linked Lists from a BST

Question Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists) Here is my ...
3
votes
2answers
123 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
2answers
3k views

Splay tree implementation

I am looking into splay trees and I implemented a version. It seems correct. Could someone please let me know if this indeed correct and how to improve it? I will just show the ...