Skip to content
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

Improved cut the array problem #70

Merged
merged 2 commits into from
Dec 7, 2016
Merged

Conversation

sergiolagunaswize
Copy link
Contributor

Problem statement has been changed, so it is clearer to the user

Also some List has been changed to ArrayList, so we have a list that has a natural sorting, because some list could have its own sorting and access implementation, causing a bigger complexity, or/and wrong output

Problem statement has been changed, so it is clearer to the user

Also some List has been changed to ArrayList, so we have a list that has a natural sorting, because some list could have its own sorting and access implementation, causing a bigger complexity, or/and wrong output
Copy link
Contributor

@zxul767 zxul767 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new description better, less noise same core problem 👍

@@ -3,15 +3,15 @@
## Interviewer Statement

```
// A cut operation substracts the lowest element of the array from each number in that array.
// A cut operation subtract the lowest element of the array from each positive number in that array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The verb "subtract" was correctly conjugated, why did you change it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what @zxul767 said. Also, if we say 'positive' we are giving the candidate more hints since the beginning IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zxul767 I did not found the word substracts anywhere, and everywhere shows me a typo when I write that word; and I think the correct word was subtracts; I'll change to that word, unless we can demonstrate that substracts is a correct word
http://www.thefreedictionary.com/subtracts
image
image

Copy link
Contributor

@zxul767 zxul767 Dec 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I meant the word "subtracts"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorgetinoco If we say: subtract the lowest element of the array from each number in that array the user might think that he only has to find the lowest element (that in the second operation is 0), and subtract that quantity to every elements of the array, causing that the program to never end (if the user makes the problem following that instruction); that's why I'm proposing to change it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zxul767 Yes, the word subtracts is the one we have to use; I'll change it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo changed

// Example of cut operation:
// array: [ 2,3,4,5,2 ]
// lowest item: 2
// array after cut operation: [ 0,1,2,3,0 ]
// Problem:
// You have an array or list of integer numbers greater than 0.
// Write a function that performs a cut operation until all the elements of the array are 0.
// The function must return the number of elements greater than 0 on each iteration
// The function must print the number of elements greater than 0 on each iteration, or return a list(or array) with those numbers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest keeping the requirement of the problem to just return a list (they can always print it in the calling function if they want to visualize it for "debugging" purposes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been changed to The function must return a list with the amount of elements greater than 0 on each iteration ; also the solution has been modified to match this problem description

@@ -38,15 +38,15 @@ The elements greater than 0 are 4.

Third iteration:
The array after the cut operation is:
`[ -1, 0, 1, -1, 0, 3 ]`
`[ 0, 0, 1, 0, 0, 3 ]`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This certainly removes noise from the core problem 👍

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are different ways to solve this problem, one is to remove the numbers instead of substract everything, other is don't substract if it is already 0 and so on. So, in my opinion is better to leave it as a substraction works, but if everyone is good with this, go ahead

@bobmazanec
Copy link
Contributor

bobmazanec commented Dec 6, 2016

I started adding this problem to our HackerRank library; how do you like this description?

@@ -85,15 +85,15 @@ public void printIterations(List<Integer> nums) {

### Better solution O(n lg n)
```
public void printIterations(List<Integer> nums) {
public void printIterations(ArrayList<Integer> nums) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the principle implement to the interface is why I did it with List instead of ArrayList.

An interface give you the ability to specify a set of behaviors that all classes that implement the interface will share in common. Consequently, we can define variables and collections (such as arrays) that don't have to know in advance what kind of specific object they will hold, only that they'll hold objects that implement the interface.

Again, if everyone is good with this, go ahead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that maybe we get some implementation of list where the sort is not natural, so we can't get the numbers in ascending order when we iterate over them, causing that we don't get the correct solution at the end;
Or maybe, we get an implementation of list where the .get(index) operation costs O(n), causing this algorithm to be an O(n^2) solution

Copy link
Contributor

@bobmazanec bobmazanec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me!

@zxul767
Copy link
Contributor

zxul767 commented Dec 7, 2016

@sergiolagunaswize thanks!

@sergiolagunaswize sergiolagunaswize merged commit ad833b8 into master Dec 7, 2016
@bobmazanec bobmazanec deleted the cut-the-array-problem branch March 31, 2017 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants