Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4259972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:51:27+00:00 2026-05-21T05:51:27+00:00

Recently in a job interview, I was given the following problem. Say I have

  • 0

Recently in a job interview, I was given the following problem.

Say I have the following table

widget_Name        |     widget_Costs      | In_Stock
---------------------------------------------------------
a                   |         15.00          |    1
b                   |         30.00          |    1
c                   |         20.00          |    1
d                   |         25.00          |    1

where widget_name is holds the name of the widget, widget_costs is the price of a widget, and in stock is a constant of 1.

Now for my business insurance I have a certain deductible. I am looking to find a sql statement that will tell me every widget and it’s price exceeds the deductible. So if my dedudctible is $50.00 the above would just return

widget_Name        |     widget_Costs      | In_Stock
---------------------------------------------------------
a                   |         15.00          |    1
d                   |         25.00          |    1

Since widgets b and c where used to meet the deductible

The closest I could get is the following

SELECT 
    *
FROM (
     SELECT 
          widget_name, 
          widget_price
     FROM interview.tbl_widgets
     minus
     SELECT widget_name,widget_price
     FROM (
          SELECT 
               widget_name,
               widget_price, 
               50 - sum(widget_price) over (ORDER BY widget_price  ROWS between unbounded preceding and current row) as running_total
          FROM interview.tbl_widgets 
     ) 
     where  running_total >= 0
)
;

Which gives me

widget_Name        |     widget_Costs      | In_Stock
---------------------------------------------------------
c                   |         20.00          |    1
d                   |         25.00          |    1

because it uses a and b to meet the majority of the deductible

I was hoping someone might be able to show me the correct answer

EDIT: I understood the interview question to be asking this. Given a table of widgets and their prices and given a dollar amount, substract as many of the widgets you can up to the dollar amount and return those widgets and their prices that remain

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:51:28+00:00Added an answer on May 21, 2026 at 5:51 am

    This looks like a Bin Packing problem these are really hard to solve especially with SQL.

    If you search on SO for Bin Packing + SQL, you’ll find how to find Sum(field) in condition ie “select * from table where sum(field) < 150” Which is basically the same problem except you want to add a NOT IN to it.

    I couldn’t get the accepted answer by brianegge to work but what he wrote about it in general was interesting

    ..the problem you
    describe of wanting the selection of
    users which would most closely fit
    into a given size, is a bin packing
    problem. This is an NP-Hard problem,
    and won’t be easily solved with ANSI
    SQL. However, the above seems to
    return the right result, but in fact
    it simply starts with the smallest
    item, and continues to add items until
    the bin is full.

    A general, more effective bin packing
    algorithm would is to start with the
    largest item and continue to add
    smaller ones as they fit. This
    algorithm would select users 5 and 4.

    So with this advice you could write a cursor to loop over the table to do just this (it just wouldn’t be pretty).

    Aaron Alton gives a nice link to a series of articles that attempts to solve the Bin Packing problem with sql but basically concludes that its probably best to use a cursor to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been asked recently in a job interview to develop an algorithm that
Recently I was asked this during a job interview. I was honest and said
A friend of mine was recently asked in a job interview to tell the
I recently botched a job interview by poorly answering a straightforward question: how do
I was asked this question recently during my job interview, and I couldn't answer
This task has already been asked/answered, but I recently had a job interview that
I recently had a techical test for a job interview where I did a
I was recently asked in a job interview to resolve a programming puzzle that
I have recently started a new job in a company that depends heavily in
recently I had a job interview. I had two tasks: 1) to refactor a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.